环境: CentOS 6.7, vim版本为7.4
一:配色
采用molokai https://github.com/tomasr/molokai
下载以后将colors/molokai.vim拷贝至~/.vim/colors
设置~/.vimrc (双引号开头为注释行)
"set colorscheme
colorscheme molokai
set t_Co=256
set background=light(暗色调可以设置为dark)
二:NERDTree
https://github.com/scrooloose/nerdtree 下载nerdtree-4.0.0.zip,解压后将doc/NERD_tree.txt拷贝至~/.vim/doc
将nerdtree_plugin/下的exec_menuitem.vim 和fs_menu.vim以及plugin/下的NERD_tree.vim拷贝至~/.vim/plugin
设置快捷键:
修改~/.vimrc,使用F2作为显示和隐藏的快捷键。
"set NERDTree
map <F2> :NERDTreeMirror<CR>
map <F2> :NERDTreeToggle<CR>
在vim中执行:NERDTree或者按F2可以打开左侧导航树。
其他快捷键:
NERDTree快捷键:
ctrl +w +w 光标自动在左右侧窗口切换ctrl +w +r 移动当前窗口的布局位置
o 展开左侧某个目录,再按一下就是合并目录
O 递归打开选中 结点下的所有目录
x 合拢选中结点的父目录
X 递归 合拢选中结点下的所有目录
go 在已有窗口中打开文件,但光标不跳到该窗口
t 中打开选中文件,并跳到新窗口
T 打开选中文件, 在原窗口
P 跳到上级目录结点
p 跳到根目录结点
q 关闭 NerdTree 窗口
i 打开选中文件,上下分屏并跳到该窗口
gi 打开选中文件,上下分屏, 不跳到该窗口
s 打开选中文件,左右分屏并跳到该窗口
gs 打开选中文件,左右分屏,不跳到该窗口
三:ctags
执行yum install ctags进行安装
在源码工程目录下执行 ctags -R ,递归的为当前目录及子目录下的所有代码文件生成tags文件,为了在各级源码目录可以识别tags, 设置~/.vimrc如下:
"set ctags
set tags=./tags;,tags;
常用命令:
用vim打开源码文件,把光标移到变量名或函数名上,然后按下“Ctrl+]”,这样就能直接跳到这个变量或函数定义的源文件中,并把光标定位到这一行(或者不移动光标,vim下使用命令:ta 变量/函数名 也能达到同样效果)。用“Ctrl+t”可以退回原来的地方。即使用户使用了N次“Ctrl+]”查找了N个变量,按N次“Ctrl+t”也能回到最初打开的文件,它会按原路返回 。
一些常用组合键:
"ctrl + t"退回到原来的地方
"[{"转到上一级的"{"
"]}"转到下级的"{"
"{"转到上一个空行
"}"转到下一个空行
"gd"转到当前光标所指的局部变量的定义。
其他更详细的ctags用法,vim中使用
:help tags
四:cscope
执行yum install cscope
在工程目录下执行:
find /XXXX -name "*.[ch]" -o -name "*.cpp" > cscope.files
cscope -Rbkq -i cscope.files
R 表示把所有子目录里的文件也建立索引
b 表示cscope不启动自带的用户界面,而仅仅建立符号数据库
q生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
k: it will not look in/usr/include for any header files that are #included in your source files (this is mainly useful when you are using Cscope with operating system and/or C library source code, as we are here).
cscope -d可以独立运行cscope搜索
使用tab键进行搜索结果和搜索菜单界面的切换。进入某个搜索结果会进入源码,进行下次搜索需要先退出当前源码,然后tab键到搜索菜单继续搜索。
如果想在执行cscope时默认打开vim编辑器,可以设置~/.bashrc
export CSCOPE_EDITOR=vim
这样默认打开vim,就可以使用vim的各种插件了。
如果想设置csope命令的别名,比如用c命令启动cscope,那么可以设置~/.bashrc
alias c='cscope'
如果想不退出当前源码继续搜索需要结合vim插件,csope配置在vim中执行:
下载http://cscope.sourceforge.net/cscope_maps.vim, 将其放在~/.vim/plugin/下面
进行级联搜索时,CTRL-t可以返回本次搜索之前的代码位置。
以下是一些快捷键,vim中执行:cscope find (或者cs f)+下面按键,例如执行:cs f g xxx可以查看xxx的定义。也可以结合ctags使用快捷键,例如将光标移动到函数或者变量类型,可以使用CTRL-\ + g查看定义。
""""""""""""" My cscope/vim key mappings " " The following maps all invoke one of the following cscope search types: " " 's' symbol: find all references to the token under cursor " 'g' global: find global definition(s) of the token under cursor " 'c' calls: find all calls to the function name under cursor " 't' text: find all instances of the text under cursor " 'e' egrep: egrep search for the word under cursor " 'f' file: open the filename under cursor " 'i' includes: find files that include the filename under cursor " 'd' called: find functions that function under cursor calls " To do the first type of search, hit 'CTRL-\', followed by one of the " cscope search types above (s,g,c,t,e,f,i,d). The result of your cscope " search will be displayed in the current window. You can use CTRL-T to " go back to where you were before the search. " nmap <C-\>s :cs find s <C-R>=expand("<cword>")<CR><CR> nmap <C-\>g :cs find g <C-R>=expand("<cword>")<CR><CR> nmap <C-\>c :cs find c <C-R>=expand("<cword>")<CR><CR> nmap <C-\>t :cs find t <C-R>=expand("<cword>")<CR><CR> nmap <C-\>e :cs find e <C-R>=expand("<cword>")<CR><CR> nmap <C-\>f :cs find f <C-R>=expand("<cfile>")<CR><CR> nmap <C-\>i :cs find i ^<C-R>=expand("<cfile>")<CR>$<CR> nmap <C-\>d :cs find d <C-R>=expand("<cword>")<CR><CR>
cscope常见问题:
1)E568: duplicate cscope database not added
当前vim版本可能已经支持cscope,并在它的配置文件中开启了cscope功能,这样就与cscope_maps.vim有了冲突。例如
$ vi /etc/vimrc
if has("cscope") && filereadable("/usr/bin/cscope")
set csprg=/usr/bin/cscope
set csto=0
set cst
set nocsverb
" add any database in current directory
if filereadable("cscope.out")
cs add $PWD/cscope.out
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
endif
可以将cscope_maps.vim中的相关配置注释掉。
$ vi ~/.vim/plugin/cscope_maps.vim
" add any cscope database in current directory
if filereadable("cscope.out")
cs add cscope.out
" else add the database pointed to by environment variable
elseif $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
使用双引号开头注释掉就可以了。
2) E567: no cscope connections
根据提示表示没有添加数据库(cscope.out),需要指定该文件。当一个工程包含子目录的时候,我们一般是在顶层目录使用cscope -Rb建立数据库的,在子目录下vim打开一个文件便会出现E567问题。所以设置一下环境变量即可。
在~/.bash_profile中写入:
$export CSCOPE_DB=/yourproject/cscope.out,然后source ~/.bash_profile使其生效。
五:taglist
从http://vim-taglist.sourceforge.net/download.html 下载taglist_46.zip,解压以后将文件放至~/.vim/下相应目录plugin和doc
配置~/.vimrc如下:
map <F3> :TlistToggle<CR> "使用F3切换打开或关闭Taglist
let Tlist_Show_One_File=1 "只显示当前文件的taglist,默认是显示多个
let Tlist_Exit_OnlyWindow=1 "如果taglist是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1 "在右侧窗口中显示taglist,各个窗口可以通过CTRL+W+W进行切换
let Tlist_WinWidth = 45 "设置显示宽度
常见问题:
1)同时显示NERDTree和taglist的时候,taglist不显示
将光标通过CTRL+W+W定位到中间的代码窗口,右侧即可显示taglist