r/emacs•
DASGUUT

Text mode: automatic newline insertion after sentences?

Hello, I use version control and diffs for all my papers in LaTeX, and therefore I try to insert newlines after every sentence I write, in order to not mess up my diffs. I often fail to do this until right before I commit, however, making it tedious to go back and insert newlines after every sentence.

I don't use auto-fill because I like to soft wrap my text. Ideally, a line should consist of a single sentence. Furthermore, a lot of functions I've looked at online require a keybinding to execute.

Is there any way to implement a function that automatically inserts punctuation after the completion of a sentence? Thanks.

Posted Jan 17, 2019, 5:18 AM UTC

35
Illiamen•

A keybinding in the mode's key map is probably the easiest way to do it.

What if you just bind the punctuataion key to a function that inserts the punctuation character followed by a newline? You could have that in the key map for whichever mode you want.

2
northrupthebandgeekQuadruple-bucky-foot-pedal-q•

Binding on the punctuation key alone would be kinda annoying for things like abbreviations (e.g. "e.g."). I'd do punctuation-space-space instead (or even just space-space).

2
DASGUUTOP•

Thanks! I had looked into abbrevs but couldn't get them to work because you can't define one for punctuation. Completely overlooked keybindings.

1
Illiamen•

The below function seems like it should work. It will insert the new line if you hit SPC after a punctation followed by a space. It could probably be improved.

(defun my-insert-space-or-newline ()
  (interactive)
  (if (looking-back "[\\.?!] " (- (point) 2) nil)
      (insert "\n")
    (self-insert-command 1)))


(define-key LaTeX-mode-map (kbd "<SPC>") #'my-insert-space-or-newline)
3
DASGUUTOP•

Thank you, this looks great.

1