21 天学会 Emacs 之第 16 天:使用 ctags 和 company-etags

优酷视频地址:

百度视频地址:

http://pan.baidu.com/s/1hr4CYBq

视频主要内容:

Spacemacs Rocks Season 2 (Day 16)

Topic: Ctags and company mode for auto completion

Why use Ctags for auto completion?

  1. Some dynamic languages don’t support syntax-aware auto completion.

    For exampel:

    Javascript (though tern-mode could do some sort of auto completion, but the configuration is complex and it’s not always reliable) && Lua

    I also use Ctags for C/C++, I usually use Emacs for writing and navigating C/C++ code, but use IDE to debug and profiling.

  2. Ctags is very fast and reliable.

How to configure ctags and auto completion?

ctags -eR . company-etags (company-etags can’t used for every major mode)

How to use Ctags effectively?

  1. project wide configurations for auto generating the Tags file.
  2. Configure the ctags rules for generating more tags
  3. use etags-select to quickly navigate a large code base

Final thoughts

When syntax-aware auto completion is not available, consider to use Ctags instead.

2 个赞

Mac下ctags setup: https://gist.github.com/nazgob/1570678

Hey Zilongshanren, thanks for your tutorials for using spacemacs. They helps a lot. I got a problem following the tutorial day 16, i.e. making the ctags and company-etags work properly. I did as the tutorial guides, below are the preparations I did that may related to it.

  • Added auto-completion to dotspacemacs-configuration-layers.
  • Folder testJs-ctags and a.js & b.js in it, same codes typed in (func1 and func2 in a.js).
  • ctags installed, ctags -e a.js or ctags -e . called, CTAGS file made, contents in it looks decent.
  • visit-tags-table set, the variable affected looks decent.

Problem is, in b.js buffer, I type in fun, only underlined hint function showed, no popdown menu with alternatives func1 or func2. Also, deminish-undo with company-mode didn’t work so I got no more info. But I did some trials to find where is not proper:

  • C-h v company-backends shows its value below:

Value:

'(company-capf
 (company-dabbrev-code company-gtags company-etags company-keywords)
 company-files company-dabbrev)

Original value was

'(company-bbdb company-nxml company-css company-eclim company-semantic company-clang company-xcode company-cmake company-capf company-files
              (company-dabbrev-code company-gtags company-etags company-keywords)
              company-oddmuse company-dabbrev)

Local in buffer .spacemacs; global value is nil

  • When cursor is at a blank position, M-x company-etags pops down the selection menu with func1() and func2(), so the tags must be correctly made and spacemacs could get that, all the problem is they don’t show up in the popdown menu when typing.

What did I miss to make it work properly? Need advice, thanks.

M-x set-variable company-backends from default (company-tern (... company-etags ...) ...) to ((company-tern ... company-etags ...) ...) solves this. It seems like among the items there is the relation of || i.e. if a former backend returns some actual results, it won’t fetch results from latter backends? But how the variable company-backends becomes like this is still unclear. Spacemacs v0.200.10 incompatible with Emacs v25.3? Or if I activated inter-incompatible layers?