为了实现类似SourceInsight功能,通过VIM+Ctags+Cscope+Taglist+Source Explore +NERD Tree实现.
一, 安装插件
1)安装Ctags 和Cscope
Ubuntu下可以直接通过命令安装:
sudo apt-get install ctags cscope
注:cscope与ctags功能类似,但能补充Ctags的不足,二者结合便能兼具ctags的便利,双能用cscope补充ctags的局限,算得上是Linux内核代码分析的强大工具.
2)安装taglist :
通过向.vimrc中添加:
Bundle 'taglist.vim'
Bundle "scrooloose/nerdtree"
Bundle "wesleyche/SrcExpl"
Bundle "majutsushi/tagbar"
再执行:BundleInstall命令即可自动安装.
3)
二,配置和使用插件
在Linux内核已经自带ctags和cscope的标签数据库的生成脚本,在内核源代码目录下通过命令可生成相应的数据库文件:
make ctags make cscope
注:前者,生成一个92M的tags文件,后者会生成cscope.files(分析文件的目录)和其它文件数据库.
配置ctags和cscope的数据库与VIM联动:
""" ctags database path """"""" set tags=/home/magc/workspace/linux-2.6.32.67/tags """ cscope database path """"""" set csprg=/usr/bin/cscope "whereis cscope set csto=0 "cscope DB search first set cst "cscope db tag DB search set nocsverb "verbose off "cs db path cs add /home/magc/workspace/linux-2.6.32.67/cscope.out set csverb "verbos off
taglist插件配置:
""""""""tag list setting """""""" filetype on nmap <F7> :TlistToggle<CR> "F7 Key = Tag list Toggling let Tlist_Ctags_Cmd = "/usr/bin/ctags" " whereis ctags let Tlist_Inc_WinWidth = 0 "window width change off" let Tlist_Exit_OnlyWindow = 0 let Tlist_Auto_Open = 0 "VIM打开时 let Tlist_Use_Right_Window = 1
Source Explorer 插件配置:
""""""" Source Explorer Setting """""" nmap <F4> :SrcExplToggle<CR> "control+h进入左边的窗口 nmap <C-H> <C-W>h "control+j进入下边的窗口 nmap <C-J> <C-W>j "control+k进入上边的窗口 nmap <C-K> <C-W>k "control+l进入右边的窗口 nmap <C-L> <C-W>l let g:Srcexpl_winHeight = 8 " // Set 100 ms for refreshing the Source Explorer let g:SrcExpl_refreshTime = 100 " // Set "Enter" key to jump into the exact definition context let g:SrcExpl_jumpKey = "<ENTER>" " // Set "Space" key for back the definition context let g:SrcExpl_gobackKey = "<SPACE>" let g:SrcExpl_isUpdateTags = 0
NERD Tree插件配置:
""""" NERD Tree Setting """"""""" let NERDTreeWinPos="left" nnoremap <F2> :NERDTreeToggle<CR>
注:上面设置中包含的快捷键有:
三,代码浏览方法
类似SourceInsight的用法,只不过这里有些窗口需要手动打开,通过F2,F4,F7打开如上图的,先在左侧的NERD文件树中找到你要查看的文件,中间大窗口是源码窗口,当光标在某变量或函数名上时,下面就会显示它的定义,右侧是当前文件的标签(变量,函数名等)
注:这里标签的来源是cscope和ctags两个数据库,有关Cscope的详细命令使用方法可参考:http://blog.csdn.net/dengxiayehu/article/details/6330200
附加1 Cscope搜索跳转技巧:(参考:http://blog.chinaunix.net/uid-24774106-id-3556337.html)
nmap <leader>sa :cs add cscope.out<cr> nmap <leader>ss :cs find s <C-R>=expand("")<cr><cr> nmap <leader>sg :cs find g <C-R>=expand("")<cr><cr> nmap <leader>sc :cs find c <C-R>=expand("")<cr><cr> nmap <leader>st :cs find t <C-R>=expand("")<cr><cr> nmap <leader>se :cs find e <C-R>=expand("")<cr><cr> nmap <leader>sf :cs find f <C-R>=expand("")<cr><cr> nmap <leader>si :cs find i <C-R>=expand("")<cr><cr> nmap <leader>sd :cs find d <C-R>=expand("")<cr><cr>