Skip to content

Configuration

Cape is available on ELPA and MELPA. You can install the package with package-install. In the following we present a sample configuration based on the popular use-package macro.

I recommend to bind the ‘cape-*’ completion commands to keys such that you can invoke them explicitly. This makes particular sense for special Capfs which you only want to trigger in rare circumstances. See the ‘:bind’ specification below.

Furthermore the ‘cape-*’ functions are Capfs which you can add to the ‘completion-at-point-functions’ list. Take care when adding Capfs to the list since each of the Capfs adds a small runtime cost. Note that the Capfs which occur earlier in the list take precedence, such that the first Capf returning a result will win and the later Capfs may not get a chance to run. In order to merge Capfs you can try the function ‘cape-capf-super’.

One must distinguish the buffer-local and the global value of the ‘completion-at-point-functions’ variable. The buffer-local value of the list takes precedence, but if the buffer-local list contains the symbol ‘t’ at the end, it means that the functions specified in the global list should be executed afterwards. The special meaning of the value ‘t’ is a feature of the ‘run-hooks’ function, see the section "Running Hooks" in the Elisp manual for further information.

emacs-lisp
;; Enable Corfu completion UI
;; See the Corfu README for more configuration tips.
(use-package corfu
  :init
  (global-corfu-mode))

;; Add extensions
(use-package cape
  ;; Bind prefix keymap providing all Cape commands under a mnemonic key.
  ;; Press C-c p ? to for help.
  :bind ("C-c p" . cape-prefix-map) ;; Alternative key: M-<tab>, M-p, M-+
  ;; Alternatively bind Cape commands individually.
  ;; :bind (("C-c p d" . cape-dabbrev)
  ;;        ("C-c p h" . cape-history)
  ;;        ("C-c p f" . cape-file)
  ;;        ...)
  :init
  ;; Add to the global default value of `completion-at-point-functions' which is
  ;; used by `completion-at-point'.  The order of the functions matters, the
  ;; first function returning a result wins.  Note that the list of buffer-local
  ;; completion functions takes precedence over the global list.
  (add-hook 'completion-at-point-functions #'cape-dabbrev)
  (add-hook 'completion-at-point-functions #'cape-file)
  (add-hook 'completion-at-point-functions #'cape-elisp-block)
  ;; (add-hook 'completion-at-point-functions #'cape-history)
  ;; ...
)