Search code examples
emacsabbreviation

Prevent Abbrev expansion after certain symbols in Emacs


Is there a way to prevent automatic expansion of an abbreviation in the built-in abbrev-mode after certain symbols? E.g. I want my abbrev to expand after whitespace, newline, comma etc., but not after a dash or underscore.

I know I can hit C-q before typing the (say) underscore, but an automatic solution would be much nicer since this occurs for me very often.

There are some abbrev hooks in the manual, but since I am a total beginner with Elisp I don't see an obvious solution...

Thank you very much!


Solution

  • OK, so a hint of the solution was already in the question itself. This is what works for me:

    (defun protect-underscore ()
     (interactive)
     (insert "_"))
    
    (defun protect-dash ()
     (interactive)
     (insert "-"))
    
    (defun protect-equal ()
     (interactive)
     (insert "="))
    
    (global-set-key (kbd "_") 'protect-underscore)
    (global-set-key (kbd "-") 'protect-dash)
    (global-set-key (kbd "=") 'protect-equal)
    

    I am sure there has to be a more elegant solution... thanks goes to Magnar.