I want to expand abbrev when I type two spaces instead of one. Is there any way to do it? Simply adding a space to the abbrev pattern doesn't work -- the abbrev is still expanded on the first space.
First, note that abbrevs expand when typing a word separator character, not just space.
Looking at the doc for
self-insert-command suggests that's no way to easily change the behavior of
self-insert-command is an interactive built-in function in ‘src/cmds.c’.
Insert the character you type. Whichever character you type to run this command is inserted. The numeric prefix argument N says how many times to repeat the insertion. Before insertion, ‘expand-abbrev’ is executed if the inserted character does not have word syntax and the previous character in the buffer does.
However, what can be done is to not use
abbrev-mode and rather bind the space key to a command that calls
expand-abbrev "on second space" and falls back to inserting a space if no abbrev is found. The following code does that.
(defun insert-space-or-expand-abbrev-on-second-space () "Insert a space or expand abbrev before current space." (interactive) (if (and (equal (char-before (point)) ? ) (not (equal (char-before (1- (point))) ? ))) ;; try to expand abbrev (progn (backward-char 1) ;; if no expansion occurs, fall back to inserting space (unless (expand-abbrev) (insert-char ? )) (forward-char 1)) ;; just insert space (insert-char ? ))) (local-set-key " " 'insert-space-or-expand-abbrev-on-second-space)
If an abbrev in indeed expanded, the second space is not inserted, but you can insert one by typing the space key one more time, since no abbrev would be found this time.
One could define something like a
two-spaces-abbrev-mode based on this code.
As mentionned by @Stefan, a different approach would be to use