Sams Teach Yourself Emacs in 24 Hours


Hour 11: Editing Utilities

Previous HourNext Hour

Sections in this Hour:


Completing Text from Another Part of the Buffer

Sometimes you type a word over and over again in a document. In the previous section, you saw how to define an abbreviation which might expand to difficult-to-type text. There are, however, situations where you do not want to define an abbreviation for a word, due to the fact that it'll take you some time to define it, and you might not know how many times you have to type this word. You might even be in a situation where you know that you have typed the word before, but you are not sure that you will type it again.

In these situation, you want Emacs to complete the word from a word earlier in the document. For this purpose dynamic abbreviations exist. Simply type part of the word and press M-/ (dabbrev-expand). Emacs then searches all your buffers for a word to complete to.

A much more powerful function called hippie-expand exists. This function expands almost anything. This includes Lisp function names, filenames from the hard disk, and text from the buffer. To use it, bind it to a key. To bind hippie-expand to meta-space, insert the following into your .emacs file :

(global-set-key "\M- " 'hippie-expand)

Now type part of a word, a filename, or a Lisp function-name, and press meta-space. If you are not happy about what it finds for you, ask it to continue its search for a completion by pressing meta-space one more time.

Note - This function is not included in XEmacs 19.

hippie-expand tries several different possibilities to find a piece of text that matches your need. Which expansions these are and the order in which they are tried can be seen in Table 11.1.

Table 11.1  Expansion Functions in hippie-expand

Number Tried




try-complete-file- name

This function completes to a filename. If several completions are possible, it selects the first one. If you are not happy about that, simply press Meta-space once more.


try-complete-file- name-partially

This function completes the filename as much as is possible, unambiguously (such as filename completion works in the minibuffer). When this function is used, the previous one must come after it in the list of try-functions.


try-expand-all- abbrevs

This function searches through all the abbreviations(described in the previous section) in all modes for a completion.



Searches the buffer for an entire line that begins exactly as the current line.

try-expand-line- all-buffers

Like try-expand-line but searches in all buffers (except the current). Thus if you want to search for a line-match first from this buffer and later from other buffers, you must first insert try-expand-line in the list, and next this one.



Tries to expand the text back to the nearest open delimiter, to a whole list from the buffer. This might be convenient when programming.

try-expand-list- all-buffers

Like try-expand-list but searches in all buffers (except the current).



Works exactly as dabbrev-expand.


try-expand-dabbrev- all-buffers

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

try-expand-dabbrev- visible

Searches the currently visible parts of all windows. This function can be put before try-expand-dabbrev-all-buffers to first try the expansions you can see.


try-expand-dabbrev- from-kill

Searches the kill ring (that is text earlier deleted with one of the kill functions) for a suitable completion of the word.

try-expand-whole- kill

Like the previous function, but inserts the whole content of the delete cell matched. This can be seen as an alternative to pressing C-y (yank) and then M-y (yank-pop) a number of times to get to the right item.


try-complete-lisp- symbol

Completes to a Lisp function name or variable name. The difference between this one and the next is the same as the difference between try-complete-file-name and try-complete-file-name-partially.


try-complete-lisp- symbol-partially

See previous item.

The expansions are implemented as Lisp functions so that a user can add an extra one in case he or she needs another expansion. This is the reason for the description of this as try-functions.

The order in which expansions are tried can be customized by setting the variable hippie-expand-try-functions-list. Simply insert the following into your .emacs file. Order the functions the way you like, with new ones added or some removed:

(setq hippie-expand-try-functions-list 

Sams Teach Yourself Emacs in 24 Hours


Hour 11: Editing Utilities

Previous HourNext Hour

Sections in this Hour: