文本补全:Corfu + Cape
2026年8月31日 2026年8月31日
说明
之前使用company,前/后端备齐。
而Corfu只作为前端。
一种用法是,Emacs配置词典,提供单词补全。
elisp
1(setq ispell-alternate-dictionary "path/to/dict")
另一种则是配合后端插件使用,如Cape。
我主要使用文件内文本补全,Emacs内置abbrev已提供。
Corfu — 补全前端
elisp
1(use-package corfu
2 :ensure t
3 :init
4 (global-corfu-mode)
5
6 :custom
7 (corfu-auto t) ; 自动弹窗补全
8 (corfu-auto-delay 0.2) ; 延迟 0.2 秒弹出
9 (corfu-auto-prefix 2) ; 输入 2 个字符后开始补全
10 (corfu-quit-no-match 'separator) ; 匹配不到时自动关闭
11 (corfu-cycle t) ; 列表循环切换
12 (corfu-preselect 'prompt) ; 默认选中提示符
13
14 (corfu-indexed-mode 1) ; 启用索引式补全
15
16 :bind
17 (:map corfu-map
18 ("C-n" . corfu-next)
19 ("C-p" . corfu-previous)
20 ("<escape>" . corfu-quit)
21 ("RET" . corfu-insert))
22 )
Cape — 后端
注意添加 :ensure t 。
elisp
1;; Add extensions
2(use-package cape
3 ;; Bind prefix keymap providing all Cape commands under a mnemonic key.
4 ;; Press C-c p ? to for help.
5 :ensure t
6 :bind ("C-c p" . cape-prefix-map) ;; Alternative key: M-<tab>, M-p, M-+
7 ;; Alternatively bind Cape commands individually.
8 ;; :bind (("C-c p d" . cape-dabbrev)
9 ;; ("C-c p h" . cape-history)
10 ;; ("C-c p f" . cape-file)
11 ;; ...)
12 :init
13 ;; Add to the global default value of `completion-at-point-functions' which is
14 ;; used by `completion-at-point'. The order of the functions matters, the
15 ;; first function returning a result wins. Note that the list of buffer-local
16 ;; completion functions takes precedence over the global list.
17 (add-hook 'completion-at-point-functions #'cape-dabbrev)
18 (add-hook 'completion-at-point-functions #'cape-file)
19 (add-hook 'completion-at-point-functions #'cape-elisp-block)
20 ;; (add-hook 'completion-at-point-functions #'cape-history)
21 ;; ...
22)
样式