Ads by Google

Sunday, September 26, 2010

Emacs Abbreviation or Autocorrection mode

Long before other editors had auto-correction,  Emacs had a simple mechanism to achieve the same.  It was called abbrevs which may be a reason why it may not be well known by new comers to Emacs.

From the comp.emacs newsgroup, here's a nice little tutorial on Emacs Abbrevs from Xah Lee.  Apart from spell corrections, you could use it as an expansion for your code snippets for any local OR global modes.  You'll soon find yourself using  C-x a i l and C-x a i g a lot to save yourself a lot of typing.  See the manual section for more info on this.

And as a bonus, you could save yourself some more time by adding the following contents to  your .abbrev file.

6 comments:

Anonymous said...

Is there a way to get emacs to make it more obvious when it applies an abbrev substitution? Perhaps printing a message to the minibuffer or temporarily highlighting the word it changed?

The abbrev feature would be more useful to me if emacs made it clear that it did the substitution---so I know to verify that it made the right correction.

Anonymous said...

Anonymous:

(add-hook 'pre-abbrev-expand-hook
#'(lambda ()
(let* ((abbrev (second (abbrev--before-point)))
(expansion (abbrev-expansion abbrev)))
(message "Expanded %s to %s" abbrev expansion))))

Anonymous said...

That hook is broken. Write the word "abbrev" and try to press space after that.

Anonymous said...

Anonymous:

My bad. I don't use abbrev-mode, so I didn't really test it on things that weren't in an abbrev table! This version will not break your emacs' abbrev-mode:

(add-hook 'pre-abbrev-expand-hook
#'(lambda ()
(let ((before-point (abbrev--before-point)))
(when before-point
(let* ((abbrev (second before-point))
(expansion (abbrev-expansion abbrev)))
(message "Expanded %s to %s" abbrev expansion))))))

Author: feel free to delete that comment so that people won't try to use it and break their abbrev-mode.

Anonymous said...

The second script provided seems to work great. Thanks for your help!

Anonymous said...

I laughed out loud when you said:

"C-x a i g a lot to save yourself a lot of typing" I think most of the words I have to type are shorter than that key sequence.

I love emacs but the keyboard shortcuts are nothing short of insane.