Aiur

zellux 的博客

Company-mode: Emacs 自动补全

Permalink

Company的全写是complete everything,它只是一个补全的前端,会自动调用semantic等后端插件。

新版的Company可以从它的官方主页(http://nschum.de/src/emacs/company-mode/)下载到,也可以从ELPA下载安装这个插件。

使用这个插件时只要在.emacs中加入

1
2
(add-to-list 'load-path "/path/to/company")
(autoload 'company-mode "company" nil t)

然后Emacs中使用M-x company-mode启动company模式即可。

具体的按键可以在company.el中看到

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(defvar company-active-map
  (let ((keymap (make-sparse-keymap)))
    (define-key keymap "\e\e\e" 'company-abort)
    (define-key keymap "\C-g" 'company-abort)
    (define-key keymap (kbd "M-n") 'company-select-next)
    (define-key keymap (kbd "M-p") 'company-select-previous)
    (define-key keymap (kbd "") 'company-select-next)
    (define-key keymap (kbd "") 'company-select-previous)
    (define-key keymap [down-mouse-1] 'ignore)
    (define-key keymap [down-mouse-3] 'ignore)
    (define-key keymap [mouse-1] 'company-complete-mouse)
    (define-key keymap [mouse-3] 'company-select-mouse)
    (define-key keymap [up-mouse-1] 'ignore)
    (define-key keymap [up-mouse-3] 'ignore)
    (define-key keymap "\C-m" 'company-complete-selection)
    (define-key keymap "\t" 'company-complete-common)
    (define-key keymap (kbd "") 'company-show-doc-buffer)
    (define-key keymap "\C-w" 'company-show-location)
    (define-key keymap "\C-s" 'company-search-candidates)
    (define-key keymap "\C-\M-s" 'company-filter-candidates)
    (dotimes (i 10)
      (define-key keymap (vector (+ (aref (kbd "M-0") 0) i))
        `(lambda () (interactive) (company-complete-number ,i))))

    keymap)
  "Keymap that is enabled during an active completion.")

这里默认的补全按键是Tab,由于已经把它绑定到了yasnippet,我用了Shift-Tab替代(Ctrl-Tab似乎在terminal下没法用,不知道哪位能解决这个问题)

1
2
(define-key company-mode-map "\t" nil)
(define-key company-mode-map [(backtab)] 'company-complete-common)

如果按了Shift-Tab没有任何反应,请确认你的后端插件已经配置正确,可以参考这篇博文

另外这里还有个视频,不喜欢看文字的话看一下这个就知道company怎么用了。http://nschum.de/src/emacs/company-mode/screencast/

评论