Skip to content

Instantly share code, notes, and snippets.

@Sebastian1011
Last active December 1, 2023 07:00
Show Gist options
  • Save Sebastian1011/12a0763461d6828c3aa3eaec1d600747 to your computer and use it in GitHub Desktop.
Save Sebastian1011/12a0763461d6828c3aa3eaec1d600747 to your computer and use it in GitHub Desktop.
Vim/Nvim C/C++ Editor

Vim C/C++

Install ctags

$ git clone https://github.com/universal-ctags/ctags.git

$ cd ctags

$ sudo apt install -y gcc make pkg-config autoconf automake python3-docutils \
    libseccomp-dev libjansson-dev libyaml-dev libxml2-dev

$ ./autogen.sh

$ ./configure --prefix=/path/to/universal-ctags #安装路径, 自己根据需要更换

$ make -j
$ sudo make install

Install Vim Plugin

vim bundle

Place this in your .vimrc:

Plugin 'ludovicchabant/vim-gutentags'
… then run the following in Vim:

:source %
:PluginInstall
For Vundle version < 0.10.2, replace Plugin with Bundle above.

vim plug

Place this in your .vimrc:

Plug 'ludovicchabant/vim-gutentags'
… then run the following in Vim:

:source %
:PlugInstall

Config ctags and vim-gutentags

Place this in your vimrc:

set tags=./.tags;,.tags 

" gutentags 搜索工程目录的标志,碰到这些文件/目录名就停止向上一级目录递归
let g:gutentags_project_root = ['.root', '.svn', '.git', '.hg', '.project']

" 所生成的数据文件的名称
let g:gutentags_ctags_tagfile = '.tags'

" 将自动生成的 tags 文件全部放入 ~/.cache/tags 目录中,避免污染工程目录
let s:vim_tags = expand('~/.cache/tags')
let g:gutentags_cache_dir = s:vim_tags

" 配置 ctags 的参数
let g:gutentags_ctags_extra_args = ['--fields=+niazS', '--extra=+q']
let g:gutentags_ctags_extra_args += ['--c++-kinds=+px']
let g:gutentags_ctags_extra_args += ['--c-kinds=+px']

" 检测 ~/.cache/tags 不存在就新建
if !isdirectory(s:vim_tags)
   silent! call mkdir(s:vim_tags, 'p')
endif

Run ctags

  1. run for single file: $ ctags <filename>
  2. run for project: $ ctags -R *
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment