Search code examples
emacssymbolsabbreviation

Emacs. Abbreving "." to ending sentence


I am an emacs newbie. I am trying to expand the character "." to ". " (period with two spaces in order to be more effective in ending a sentence in emacs) with abbrevs. In other words, when I type a "." followed by a space, emacs put a ". ".

I have put the next code in my abbrevs file, but it doesn't work.

(text-mode-abbrev-table)                                    
"."            0    ".    " 

Anybody can help me?


Solution

  • I'm not sure why you would want this, but here it is:

    Put this in ~/.emacs:

    (defun electric-dot ()
      (interactive)
      (if (and (looking-back "\\w") (not (looking-back "[0-9]")))
          (progn
            (self-insert-command 1)
            (insert "  "))
        (self-insert-command 1)))
    
    (defvar electric-dot-on-p nil)
    
    (defun toggle-electric-dot ()
      (interactive)
      (global-set-key
       "."
       (if (setq electric-dot-on-p
                 (not electric-dot-on-p))
           'electric-dot
         'self-insert-command)))
    

    Afterwards, use M-xtoggle-electric-dot to make each . insert . , if it's after a word. You can call it again to restore the default behavior.

    As a side-note, there's a ton of much better ways to improve your text input speed e.g. auto-complete-mode. You can install it with package-install.

    UPD electric-dot inserts just a dot after digits.

    UPD Here's electric-space instead:

    This one will insert an extra space if it's looking back at a word followed by a dot.

    (defun electric-space ()
      (interactive)
      (if (looking-back "\\w\\.")
          (insert " "))
      (self-insert-command 1))
    
    (defvar electric-space-on-p nil)
    
    (defun toggle-electric-space ()
      (interactive)
      (global-set-key
       " "
       (if (setq electric-space-on-p
                 (not electric-space-on-p))
           'electric-space
         'self-insert-command)))