Thursday, December 27, 2007

Emacs Tip #5: hippie-expand

One thing about Emacs is that it's always trying to save you from typing. Sure, there are jokes about repetitive strain injury, but, in fact, Emacs is trying to save you from typing.

One of the cooler built-in completion commands is 'dabbrev-expand, bound to M-/ by default.

What 'dabbrev-expand does is that it looks at the word you're currently typing and tries to expand it to match one a word you've already typed. It searches the text before the current point, then the text after the current point, and then it searches through other buffers. Repeated M-/ cycle through the possible completions.

A better binding is hippie expand:

(global-set-key (kbd "M-/") 'hippie-expand)

What 'hippie-expand adds to the mix is that a variety of completion functions, including the above-mentioned 'dabbrev-expand, along with others like expanding file names, expanding from the kill ring, etc.

For more documentation, you can read the info page:
C-h F hippie-expand RET
The documentation in the code for hippie-expand is useful (the *info* pages point you there. To read that do:
M-x find-library hippie-exp RET

My preferred setting for the 'hippie-expand functions is the following:

(setq hippie-expand-try-functions-list '(try-expand-dabbrev try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill try-complete-file-name-partially try-complete-file-name try-expand-all-abbrevs try-expand-list try-expand-line try-complete-lisp-symbol-partially try-complete-lisp-symbol))

1 comment:

awhan said...

i just tried it out and it rocks ... i wrote a long expression and then two lines later when i needed it i hit M-/ and wallah the magic of "try-expand-list" created a big grin on my face :D