Emacs 自动补全,最好的插件当属 ycmd。以下记录我的安装过程。
1. 安装 ycmd server
github 官方地址: https://github.com/Valloric/ycmd
- git 克隆 ycmd 到本地 ~/ycmd
$ git clone https://github.com/Valloric/ycmd.git ~/ycmd
- 安装必要的编译工具和库
$ sudo apt-get install build-essential cmake python-dev
- 下载完整的 submodule (比较大哦。。。)
$ git submodule update --init --recursive
- 编译 (比较慢哦。。。)
$ cd ~/ycmd
$ ./build.py --all
编译完成之后,会在当前目录下生成一个 ycmd 文件夹,现在就像这样 ~/ycmd/ycmd
2. 安装 ycmd client,还有必要的工具
- 打开 Emacs,命令 M-x list-packages, 分别安装 ycmd, company-ycmd, flycheck-ycmd
3. 配置,如下内容添加到 ~/.emacs
;;;;============================================================================================ < for ycmd start >
;;;; To use ycmd-mode in all supported modes
(require ‘ycmd)
(add-hook ‘after-init-hook #‘global-ycmd-mode)
;;;; Specify only support c/c++ mode
;;(require ‘ycmd)
;;(add-hook ‘c++-mode-hook ‘company-mode)
;;(add-hook ‘c++-mode-hook ‘ycmd-mode)
;;;; Specify how to run the server
(set-variable ‘ycmd-server-command ‘("/usr/bin/python" "/home/peterpan/ycmd/ycmd"))
;;(set-variable ‘ycmd-server-command ‘("/usr/bin/python" "~/ycmd/ycmd")) ;; Cannot use ~, should use abspath
;;;; Specify a global emacs configuration
(set-variable ‘ycmd-global-config "/home/peterpan/ycmd/examples/.ycm_extra_conf.py")
;;(set-variable ‘ycmd-global-config "~/ycmd/examples/.ycm_extra_conf.py") ;; Cannot use ~, should use abspath
;;;; Completion framework
(require ‘company-ycmd)
(company-ycmd-setup)
(add-hook ‘after-init-hook #‘global-company-mode)
;;;; Enable flycheck
(require ‘flycheck-ycmd)
(flycheck-ycmd-setup)
(add-hook ‘after-init-hook #‘global-flycheck-mode)
;;;; Set always complete immediately
(setq company-idle-delay 0)
;;;;============================================================================================ < for ycmd end >
有个问题: 补全的时候,弹出 tip 窗口,不能按 Tab 或者 Ctrl + n 键去 cycle 可选项,只能继续输入,直到第一个就是要用的,之后按 Enter 选中。你要知道怎么解决这个问题,请告诉我 :P
完。