To install this package, run in Emacs:
M-x package-install RET corfu RET
Corfu enhances completion at point with a small completion popup. The current
candidates are shown in a popup below or above the point. Corfu is the
completion-in-region counterpart of the Vertico minibuffer UI.
Corfu is a small package, which relies on the Emacs completion facilities and
concentrates on providing a polished completion UI. Completions are either
provided by commands like
dabbrev-completion or by pluggable backends
completion-at-point-functions, Capfs). Most programming language major modes
implement a Capf. Furthermore the language server packages, Eglot and
Lsp-mode, use Capfs which talk to the LSP server to retrieve the completions.
Corfu does not include its own completion backends. The Emacs built-in Capfs
and the Capfs provided by other programming language packages are usually
sufficient. A few additional Capfs and completion utilities are provided by
the Cape package.
NOTE: Corfu uses child frames to show the popup. For now Corfu falls back to
the default setting of the
completion-in-region-function on non-graphical
TABat any time.
TABand selected with
Corfu is available from GNU ELPA, such that it can be installed directly via
package-install. After installation, the global minor mode can be enabled with
M-x corfu-global-mode. In order to configure Corfu and other packages in your
init.el, you may want to use
Corfu is highly flexible and customizable via
corfu-* customization variables.
For filtering I recommend to give Orderless completion a try, which is
different from the familiar prefix TAB completion. Corfu can be used with the
default completion styles, the use of Orderless is not a necessity. See also
the Corfu Wiki for additional configuration tips. In particular the Lsp-mode
configuration is documented in the Wiki.
Here is an example configuration:
(use-package corfu ;; Optional customizations ;; :custom ;; (corfu-cycle t) ;; Enable cycling for `corfu-next/previous' ;; (corfu-auto t) ;; Enable auto completion ;; (corfu-commit-predicate nil) ;; Do not commit selected candidates on next input ;; (corfu-quit-at-boundary t) ;; Automatically quit at word boundary ;; (corfu-quit-no-match t) ;; Automatically quit if there is no match ;; (corfu-preview-current nil) ;; Disable current candidate preview ;; (corfu-preselect-first nil) ;; Disable candidate preselection ;; (corfu-echo-documentation nil) ;; Disable documentation in the echo area ;; (corfu-scroll-margin 5) ;; Use scroll margin ;; You may want to enable Corfu only for certain modes. ;; :hook ((prog-mode . corfu-mode) ;; (shell-mode . corfu-mode) ;; (eshell-mode . corfu-mode)) ;; Recommended: Enable Corfu globally. ;; This is recommended since dabbrev can be used globally (M-/). :init (corfu-global-mode)) ;; Optionally use the `orderless' completion style. See `+orderless-dispatch' ;; in the Consult wiki for an advanced Orderless style dispatcher. ;; Enable `partial-completion' for files to allow path expansion. ;; You may prefer to use `initials' instead of `partial-completion'. (use-package orderless :init ;; Configure a custom style dispatcher (see the Consult wiki) ;; (setq orderless-style-dispatchers '(+orderless-dispatch) ;; orderless-component-separator #'orderless-escapable-split-on-space) (setq completion-styles '(orderless) completion-category-defaults nil completion-category-overrides '((file (styles . (partial-completion)))))) ;; Use dabbrev with Corfu! (use-package dabbrev ;; Swap M-/ and C-M-/ :bind (("M-/" . dabbrev-completion) ("C-M-/" . dabbrev-expand))) ;; A few more useful configurations... (use-package emacs :init ;; TAB cycle if there are only few candidates (setq completion-cycle-threshold 3) ;; Emacs 28: Hide commands in M-x which do not apply to the current mode. ;; Corfu commands are hidden, since they are not supposed to be used via M-x. ;; (setq read-extended-command-predicate ;; #'command-completion-default-include-p) ;; Enable indentation+completion using the TAB key. ;; `completion-at-point' is often bound to M-TAB. (setq tab-always-indent 'complete))
See also the Corfu Wiki for additional configuration tips. For more general documentation read the chapter about completion in the Emacs manual. If you want to create your own Capfs, you can find documentation about completion in the Elisp manual.
Corfu can be used for completion in the minibuffer, since it relies on child
frames to display the candidates. By default,
corfu-global-mode does not
corfu-mode in the minibuffer, to avoid interference with specialised
minibuffer completion UIs like Vertico or Mct. However you may still want to
enable Corfu completion for commands like
shell-command), which read from the minibuffer. Activate
corfu-mode only if
completion-at-point is bound in the minibuffer-local keymap to achieve this
(defun corfu-enable-in-minibuffer () "Enable Corfu in the minibuffer if `completion-at-point' is bound." (when (where-is-internal #'completion-at-point (list (current-local-map))) ;; (setq-local corfu-auto nil) Enable/disable auto completion (corfu-mode 1))) (add-hook 'minibuffer-setup-hook #'corfu-enable-in-minibuffer)
You can also enable Corfu more generally for every minibuffer, as long as no other completion UI is active. If you use Mct or Vertico as your main minibuffer completion UI, the following snippet should yield the desired result.
(defun corfu-enable-always-in-minibuffer () "Enable Corfu in the minibuffer if Vertico/Mct are not active." (unless (or (bound-and-true-p mct--active) (bound-and-true-p vertico--input)) ;; (setq-local corfu-auto nil) Enable/disable auto completion (corfu-mode 1))) (add-hook 'minibuffer-setup-hook #'corfu-enable-always-in-minibuffer 1)
When completing in the Eshell I recommend conservative local settings, no auto completion, quitting at boundary and quitting if there is no match.
(add-hook 'eshell-mode-hook (lambda () (setq-local corfu-quit-at-boundary t corfu-quit-no-match t corfu-auto nil) (corfu-mode)))
Shell completion uses the flexible
pcomplete mechanism internally, which allows
you to program the completions per shell command. If you want to know more, look
into this blog post, which shows how to configure pcomplete for git commands.
Unfortunately pcomplete has a few technical issues, which we can work around
with the Cape library (Completion at point extensions). Cape provides wrappers,
which sanitize the pcomplete function. Ideally the bugs in pcomplete should be
fixed upstream. For now these two advices are strongly recommended to achieve a
sane Eshell experience.
;; Silence the pcomplete capf, no errors or messages! (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-silent) ;; Ensure that pcomplete does not write to the buffer ;; and behaves as a pure `completion-at-point-function'. (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-purify)
You may be interested in configuring Corfu in TAB-and-Go style. Pressing TAB moves to the next candidate and further input will then commit the selection.
(use-package corfu ;; TAB-and-Go customizations :custom (corfu-cycle t) ;; Enable cycling for `corfu-next/previous' (corfu-preselect-first nil) ;; Disable candidate preselection ;; Use TAB for cycling, default is `corfu-complete'. :bind (:map corfu-map ("TAB" . corfu-next) ([tab] . corfu-next) ("S-TAB" . corfu-previous) ([backtab] . corfu-previous)) :init (corfu-global-mode))
Corfu uses a transient keymap
corfu-map which is active while the popup is shown.
The keymap defines the following remappings and bindings:
Corfu works well together with all packages providing code completion via the
completion-at-point-functions. Many modes and packages already provide a Capf
out of the box. Nevertheless you may want to look into complementary packages
to enhance your setup.
completion-in-regioncommands in my Cape package. The package provides a file path, a dabbrev completion backend and a backend which allows you to enter unicode characters in the form of TeX commands. Cape provides an adapter to reuse Company backends in Corfu. Furthermore the function
cape-super-capfcan merge/groups multiple Capfs, such that the candidates of multiple Capfs are displayed together at the same time.
company-quickhelp. Note that the corfu-doc package is new and still work in progress.
completion-at-pointcommand (or the
completion-in-regionfunction) does not invoke Company.
consult-completion-in-regionwhich can be set as
completion-in-region-functionsuch that it handles
completion-at-point. The function works by transferring the in-buffer completion to the minibuffer. In the minibuffer, the minibuffer completion UI, for example Vertico takes over. If you prefer to perform all your completions in the minibuffer
consult-completion-in-regionis your best option.
Corfu is robust in most scenarios. There are a few known technical caveats.
completion-in-region-functionon non-graphical displays. You can use one of the alternatives in terminals.
completion-at-pointdoes not maintain a history (See branch
historyfor a possible solution).
Since this package is part of GNU ELPA contributions require a copyright assignment to the FSF.