emacs: text expansion

There are several built-in tools – and just as many third-party ones – in Emacs that expand text. All of them serve a slightly different purpose, but the goal is to minimize typing and maximize automation.

  • Abbrev
  • DAbbrev
  • Hippie expand
  • Skeletons
  • Tempo
  • YASnippet
  • Autoinsert

Abbrev

Key Binding Description
C-x a l Adds mode-specific abbrev
C-x a g Adds global abbrev
C-x a i g Adds mode-specific inverse abbrev
C-x a i l Adds global inverse abbrev

DAbbrev and Hippie Expand

Key Binding Description
M-/ Expands word at the point using M-x dabbrev-expand
C-M-/ Expands as much as possible, and shows a list of possible completions

DAbbrev is not smart. It looks at other words in your buffer and it attempts to complete the word at the point to one of those. That does not make it useless – it is still useful – it’s just that Hippie Expand is so much better.

To use Hippie Expand effectively, you should replace DAb- brev as the two – though it’s possible to use both – really don’t complement one another at all. Add this to your init file to switch to Hippie Expand:

(global-set-key [remap dabbrev-expand] 'hippie-expand)

Hippie Expand expands more than just words. The variable hippie-expand-try-functions-list is an ordered list of expan- sion functions Hippie Expand will call with the text at the point when you call M-/.

What I like most about Hippie Expand is the file name com- pletion. It works exactly like your shell’s TAB-completion: you type M-/ and Hippie Expand will try to complete the filename or directory at the point. If you ever find yourself inserting absolute paths or relative file names in code, config- uration files or documentation — Hippie Expand will make your life much easier.

Another great feature is its ability to complete whole lines. It will fall back to word completion if it runs out of ideas, and if you regularly write elisp, then Hippie Expand will guess if the text at the point is a potential elisp symbol and automatically complete it for you also.