Tailoring Emacs Completions with Hippie-Expand

Emacs has advanced built-in completion features. Initially it used dabbrev for completion. This was later superseded by hippie-expand which incorporates dabbrev functionality and expands on it, making it far more flexible.

Before one turns to external completion packages like company and yasnippet one should test and customize hippie-expand which complements Emacs abbrev-mode.

hippie-expand is a single function for a lot of different kinds of completions and expansions.

Invoked repeatedly it tries all possible completions in order.

Which kind of completions to try, and in which order, is determined by the contents of this variable: hippie-expand-try-functions-list.

Given a positive numeric argument, M-x hippie-expand jumps directly that number of functions forward in this list.

C-u undoes the tried completion.

The default values are:

try-complete-file-name-partially
try-complete-file-name
try-expand-all-abbrevs
try-expand-list
try-expand-line
try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-lisp-symbol-partially
try-complete-lisp-symbol

One can customize hippie-expand by changing the order of, removing, or inserting new functions in this list.

To see in the echo area what value hippie-expand is using:

(setq hippie-expand-verbose t)

A short description of the more important values are:

try-complete-file-name-partially: completes only as many characters as are common to all possibilities.

try-complete-file-name: goes through all possible completions instead of just completing as much as is unique.

Example: If we have three files: example1.txt example2.txt example3.txt: partial file name completion exa expands to example, while complete-file-names goes through all possibilities.

c:/tarzan/examples/example3.txt

try-expand-all-abbrevs: can be removed if you do not use abbrevs or do not want to use abbrevs with hippie-expand. This gives you an on-demand expansion mechanism for those abbreviations that you may not wish to always expand automatically.

try-expand-line: Searches the buffer for an entire line that begins as the current line. This is akin to C-x C-l in Vim. It is most convenient when you want to replicate or near replicate lines in a buffer. Not useful for prose or when most of your lines are unique.

#include <stdio.h>
#include <stdio.h>

try-expand-list: Tries to expand the text back to the nearest open delimiter, to a whole list from the buffer. Convenient mostly when writing Lisp or TeX.

try-expand-dabbrev: works exactly as dabbrev-expand whose primary purpose is word completion. The vim equivalent is C-n

try-expand-dabbrev-all-buffers: like dabbrev-expand but searches all Emacs buffers (except the current) for matching words.

try-complete-lisp-symbol-partially: Only completes the symbol up to the longest common prefix, without guessing the rest of the symbol.

My settings:

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

  (setq hippie-expand-verbose t)

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

Return to Home