Emacs - Company
Table of Contents
Introduction
Auto-completion is an important functionality in Emacs. It can greatly speed up our editing and programming. Generally speaking, the auto-completion can be divided into two parts, frontend and backend. As their names indicate, the former presents interface, while the latter provides contents. In a nutshell, the backend generates completion contents and feeds them to the frontend; then the frontend presents completion candidates to us for selection.
Up to now, there are a couple of popular frontends, auto-complete
1 and company
2. The word "company" stands for "complete anything". In fact, their functionalities are very similar. Both of them work well and their developments are fairly active. Both of them come with a variety of built-in backends. What's more, you can customize your own backends for your special requirements.
It should be noticed that each of the both above provides backend for C/C++ programming, e.g. company-clang
is a powerful backend based on clang
. It enables company completing based on the semantic analysis provided by clang. With the aid of this backend, the programming experience and efficiency can be remarkably improved.
For myself, I have been using company since I began to use Emacs. Therefore, I just summarize the installation and configuration of company in this post.
Installation and configuration
Company
Company can be directly installed via ELPA.
M-x package-install company
After the installation, following configurations should be added in ~/.emacs
.
;; enable global company mode (add-hook 'after-init-hook 'global-company-mode) ;; completion immediately (setq company-idle-delay 0) ;; move the selection to the next/previous candidate using C-n/p instead of M-n/p (with-eval-after-load 'company (define-key company-active-map (kbd "M-n") nil) (define-key company-active-map (kbd "M-p") nil) (define-key company-active-map (kbd "C-n") #'company-select-next) (define-key company-active-map (kbd "C-p") #'company-select-previous))
Clang support
Clang support is out-of-the-box. Additional package installation is not necessary. However, company-semantic is also built-in and precedes company-clang. Therefore, it is necessary to remove company-semantic. To this end, following configurations are needed.
;; remove company-semantic from company-backends. (setq company-backends (delete 'company-semantic company-backends)) ;; make company-clang find IT++ headers and support c++11. (setq company-clang-arguments (quote ("-I/opt/itpp/include/" "-std=c++14")))
C headers support
For the sake of completing C/C++ header files while input, package company-c-headers
can be installed by
M-x package-install company-c-headers
and configured by
(add-to-list 'company-backends 'company-c-headers) (setq company-c-headers-path-system (quote ("/usr/include/" "/usr/local/include/" "/usr/include/c++/6.1.1/" "/opt/itpp/include/")))
LaTeX support
In order to enjoy the completion in LaTeX composition, package company-auctex
can be installed3.
M-x package-install company-auctex
Accordingly, append the following line to the configuration of company in ~/.emacs
.
;; enable company backends for LaTeX (based on AucTeX) (company-auctex-init)
Python support
Targetting for Python support, package company-jedi
needs to be installed.
M-x package-install company-jedi
Likewise, add following lines to the configuration of company in ~/.emacs
.
(add-to-list 'company-backends 'company-jedi)
Footnotes:
AucTeX will also be installed as a dependency.