您的位置:首页 > 运维架构 > Linux

Linux查看代码工具vim+ctags+cscope+taglist

2014-10-23 20:31 696 查看
vim:linux下强大的编辑器。

ctags:生成tag文件的命令,这个是其他工具的基础。

ctags是一个linux上很普遍的源码分析工具, 可以将代码中的函数变量等定义的位置记录在一个名称为tags的文件. 类似于数据库记录功能. tags文件的产出最简单的方法是在需要生成tags的工程项目的根目录下执行ctags -R命令, 这会调用tags递归的扫描当前目录以及所有子目录中可以被tags识别的文件所以文件数据信息都会汇集到tags文件中.

ctags的优点是使用简单, 生成的tags文件比较小, 使用时对tags的检索也相对比较快, 对c语言的函数和宏定义跳转相当准确高效. 他的缺点功能相对单一, 没办法实现对一个关键字出现位置的统计, 一个函数被调用的位置统计, 局部变量的定义跳转往往没有效果等. 我们在编码的时候除了随时查看函数定义的需求以外, 另一个比较常用的需求是对一个函数在项目中出现位置查找. 这个功能ctags是没有的, 为了让vim实现这个功能, 我们需要借助另一tags索引工具:cscope

taglist:需要ctags的支撑,直接在左边列出函数列表,全局参数列表。

cscope:比较强大,可以对函数以及部分类型定义进行跳转。其实cscope是来代替ctags的功能的。

工具的安装

vim在一般的linux系统中都存在,一般不需要安装。

安装ctags和cscope:

sudo apt-get install ctags

sudo apt-get install cscope

安装taglist:

taglist是vim的一个源代码浏览插件,不需要安装。只需要加入两个配置文件和在.vimrc中添加相应的配置即可。

可以到 http://www.vim.org/scripts/script.php?script_id=273上下载最新的taglist插件。或者从 href="http://www.vim.org/scripts/download_script.php?src_id=7701" target=_blank>http://www.vim.org/scripts/download_script.php?src_id=7701直接下载。

插件中有两个文件,一个taglist.vim,另一个是taglist.txt。

在HOME下建立一个.vimrc文件和.vim文件夹,.vim文件夹里面建立doc和plugin两个文件夹,把上一步得到的taglist.vim拷贝到doc文件夹里,taglist.txt拷贝到plugin文件夹里。

vim的使用

.vimrc的配置

在.vimrc文件中输入以下内容:

".vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"For ctags, then it can find the 'tags' file even not in current directory
set tags=tags;/
"Get out of VI's compatible mode..
set nocompatible
"Sets how many lines of history VIM har to remember
set history=400
"Set to auto read when a file is changed from the outside
set autoread
"Have the mouse enabled all the time:
"when you need to copy from vim, maybe you have to ':set mouse=' first
set mouse=a
"""""""""""""""""""""""""""""""""""""
" Colors and Fonts
"""""""""""""""""""""""""""""""""""""
"Enable syntax highlight
syntax enable
"set colorscheme
colorscheme elflord
"endif
"""""""""""""""""""""""""""""""""""""
" VIM userinterface
"""""""""""""""""""""""""""""""""""""
"Set 7 lines to the curors away from the border- when moving vertical..
set so=7
"Turn on WiLd menu
set wildmenu
"Always show current position
set ruler
"The commandbar is 2 high
set cmdheight=2
"Show line number
set nu
"Set backspace
set backspace=eol,start,indent
"Bbackspace and cursor keys wrap to
set whichwrap+=<,>,h,l
"show matching bracets
set showmatch
"How many tenths of a second to blink
set mat=2
"Highlight search things
set hlsearch
"imediately show the search result
set is
"""""""""""""""""""""""""""""""""""""
" Folding
"""""""""""""""""""""""""""""""""""""
"Enable folding, I find it very useful
set nofen
set fdl=0
"""""""""""""""""""""""""""""""""""""
" Text options
"""""""""""""""""""""""""""""""""""""
set expandtab
set shiftwidth=2
set ambiwidth=double
set smarttab
"Set Tab=4 spaces
set ts=4
set lbr
set tw=500
set selection=inclusive
""""""""""""""""""""""""""""""
" Indent
""""""""""""""""""""""""""""""
"Auto indent
set ai
"Set auto indent width = 4 spaces
set sw=4
"Smart indet
set si
"C-style indenting
set cindent "usage: select codes, press '=' key, the codes will autoindenting
"Wrap lines
set wrap
"Encoding settings
if has("multi_byte")
" Set fileencoding priority
if getfsize(expand("%")) > 0
set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,latin1
else
set fileencodings=cp936,big5,euc-jp,euc-kr,latin1
endif
" CJK environment detection and corresponding setting
if v:lang =~ "^zh_CN"
" Use cp936 to support GBK, euc-cn == gb2312
set encoding=cp936
set termencoding=cp936
set fileencoding=cp936
elseif v:lang =~ "^zh_TW"
" cp950, big5 or euc-tw
" Are they equal to each other?
set encoding=big5
set termencoding=big5
set fileencoding=big5
elseif v:lang =~ "^ko"
" Copied from someone's dotfile, untested
set encoding=euc-kr
set termencoding=euc-kr
set fileencoding=euc-kr
elseif v:lang =~ "^ja_JP"
" Copied from someone's dotfile, unteste
set encoding=euc-jp
set termencoding=euc-jp
set fileencoding=euc-jp
endif
" Detect UTF-8 locale, and replace CJK setting if needed
if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set encoding=utf-8
set termencoding=utf-8
set fileencoding=utf-8
endif
else
echoerr "Sorry, this version of (g)vim was not compiled with multi_byte"
endif
"""""""""""""""""""""""""""""""""""""
"plugins
"""""""""""""""""""""""""""""""""""""
" Tlist
if &diff
let Tlist_Auto_Open=0 "don't auto pen when compare two files
else
let Tlist_Auto_Open=1 "auto pen Tlist when open a file
endif
"set taglist window in right, delete the following line if you don't like
let Tlist_Use_Right_Window=1
let Tlist_Auto_Update=1
let Tlist_File_Fold_Auto_Close=1
"auto close Tlist when exiting file.
let Tlist_Exit_OnlyWindow = 1
nmap <F7> :copen<CR>
nmap <F6> :cclose<CR>
6
nnoremap <silent> <F8> :TlistToggle<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" cscope setting
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if has("cscope")
set csprg=/usr/bin/cscope
set csto=1
set cst
set nocsverb
" add any database in current directory
if filereadable("cscope.out")
cs add cscope.out
endif
set csverb
endif

" go to the last position
if has("autocmd")
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
\| exe "normal g'\"" | endif
endif

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>


ctags的使用

如果需要浏览某个目录下的源码,需要进入源码目录并使用以下命令来生成索引文件:

ctags -R

其中"-R"表示递归创建,也就包括源代码根目录下的所有子目录下的源程序。然后使用 vim打开一个C文件。使用F8来打开taglist的窗口(由于在.vimrc中加入了语句nnoremap <silent> <F8> :TlistToggle<CR>),如果要想知道某个人函数或者结构体在什么地方,只要将光标移动到函数上,然后按住 CTRL+]即可(或者CTRL+鼠标左键),看完后安CTRL+O又会回到原来的地方。在左边的tlist窗口和右边正常的编辑区切换用 ctrl+2w个,ctrl+t 返回跳转到标签的前一次位置(即上一个标签)。

如果你的vim有使用echofunc插件来显示函数的参数定义, 那么在使用ctags生产索引文件时需要使用如下附加参数:

ctags -R --fields=+lS

ctags默认生成的索引文件只包含了对C语言的语法分析, 如果你需要ctags支持对C++语法分析. 需要使用下面的命令:

ctags -R --c++-kinds=+p --fields=+iaS --extra=+q

为了让vim对tags的支持更加自动化, 可以在在.vimrc中添加 settags+=./tags。这样就指定了解析的tag文件包含当前目录下的tags文件。

将光标置于函数名上,shift+k 查看Linux库函数。

cscope的使用

生成索引文件的命令:

cscope -Rbkq

R 表示把所有子目录里的文件也建立索引

b 表示cscope不启动自带的用户界面,而仅仅建立符号数据库

q 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度

k 在生成索引文件时,不搜索/usr/include目录

常用命令:

:cs find s ---- 查找C语言符号,即查找函数名、宏、枚举值等出现的地方

:cs find g ---- 查找函数、宏、枚举等定义的位置,类似ctags所提供的功能

:cs find d ---- 查找本函数调用的函数:cs find c ---- 查找调用本函数的函数

:cs find t: ---- 查找指定的字符串

:cs find e ---- 查找egrep模式,相当于egrep功能,但查找速度快多了

:cs find f ---- 查找并打开文件,类似vim的find功能

:cs find i ---- 查找包含本文件的文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: