I do a lot of natural text editing in Emacs. And by editing I mean editing, i.e., it is often the case that I edit texts written by someone else. This is a fine distinction: for instance, when I type the text myself, I usually put the commas where they belong, but when I edit someone else’s text, I often need to insert a comma where it should be.
Imagine this situation:
You are in a maze of twisty little passages all alike.-!-
where -!-
denotes the point. Notice the missing comma before “all alike”.
In stock Emacs, you’d have to go back two words (e.g., press M-b
twice), then go back one character (C-b
), and then press the comma.
However, how often do you want to put the comma after the space, i.e. type something like this: foo ,bar
? Eh? So I don’t see a reason a comma pressed when the point is after a space shouldn’t put the comma before that space.
So, here’s a snippet of code doing exactly that. You can bind it to any punctuation you want to be always inserted before a space (or a string of more spaces, for that matter), not only the comma.
(defun smart-self-insert-punctuation (count) "If COUNT=1 and the point is after a space, insert the relevant character before any spaces." (interactive "p") (if (and (= count 1) (eq (char-before) ?\s)) (save-excursion (skip-chars-backward " ") (self-insert-command 1)) (self-insert-command count))) (global-set-key "," #'smart-self-insert-punctuation)
Of course, you can still put the comma exactly where the point is by means of C-q ,
. (Also, if you use a prefix argument to smart-self-insert-punctuation
, no space trickery is involved.)
If you feel that global-set-key
is too much, you can of course bind this command in any map you want, for instance
(eval-after-load 'tex '(define-key TeX-mode-map "," 'smart-self-insert-punctuation))
Now it is enough to move to the beginning of the word “all” and press the comma, which will then “magically” jump before the space. (Try it out, it looks cool!)
This is not a huge productivity gain, of course, but multiply it by a few dozen missing commas per article, and then by several dozens of articles per year. Not only the time saved becomes important, but the decrease in irritation caused by pressing the same keys over and over again.
And now that I have this, I think I could do a similar thing with C-t
, so that it transposes two chars before the point (as opposed as two chars around the point) if there is a space before the point.