8.4.4 How Completion Alternatives Are Chosen
Completion commands work by narrowing a large list of possible completion alternatives to a smaller subset that matches what you have typed in the minibuffer. In Completion Example, we gave a simple example of such matching. The procedure of determining what constitutes a match is quite intricate. Emacs attempts to offer plausible completions under most circumstances.
Emacs performs completion using one or more completion stylesβsets of criteria for matching minibuffer text to completion alternatives. During completion, Emacs tries each completion style in turn. If a style yields one or more matches, that is used as the list of completion alternatives. If a style produces no matches, Emacs falls back on the next style.
The list variable completion-styles
specifies the completion styles to use. Each list element is the name of a completion style (a Lisp symbol). The default completion styles are (in order):
basic
β
A matching completion alternative must have the same beginning as the text in the minibuffer before point. Furthermore, if there is any text in the minibuffer after point, the rest of the completion alternative must contain that text as a substring.
partial-completion
β
This aggressive completion style divides the minibuffer text into words separated by hyphens or spaces, and completes each word separately. (For example, when completing command names, βem-l-m
β completes to βemacs-lisp-mode
β.)
Furthermore, a β*
β in the minibuffer text is treated as a wildcardβit matches any string of characters at the corresponding position in the completion alternative.
emacs22
β
This completion style is similar to basic
, except that it ignores the text in the minibuffer after point. It is so-named because it corresponds to the completion behavior in Emacs 22.
The following additional completion styles are also defined, and you can add them to completion-styles
if you wish (see Customization):
substring
β
A matching completion alternative must contain the text in the minibuffer before point, and the text in the minibuffer after point, as substrings (in that same order).
Thus, if the text in the minibuffer is βfoobar
β, with point between βfoo
β and βbar
β, that matches βafoobbarc
β, where a
, b
, and c
can be any string including the empty string.
flex
β
This aggressive completion style, also known as flx
or fuzzy
or scatter
completion, attempts to complete using in-order substrings. For example, it can consider βfoo
β to match βfrodo
β or βfbarbazoo
β.
initials
β
This very aggressive completion style attempts to complete acronyms and initialisms. For example, when completing command names, it matches βlch
β to βlist-command-history
β.
There is also a very simple completion style called emacs21
. In this style, if the text in the minibuffer is βfoobar
β, only matches starting with βfoobar
β are considered.
You can use different completion styles in different situations, by setting the variable completion-category-overrides
. For example, the default setting says to use only basic
and substring
completion for buffer names.