linux中用vim+ctags+cscope+taglist查看源代码

1. 工具介绍。
vim:linux下强大的编辑器。

ctags:生成tag文件的命令,这个是其他工具的基础。
taglist:需要ctags的支撑,直接在左边列出函数列表,全局参数列表。
cscope:比较强大,可以对函数以及部分类型定义进行跳转。其实cscope是来代替ctags的功能的。

2.安装工具。

(1)安装ctags和cscope比较简单,只要在终端下敲一下命令就ok了!

sudo apt-get install ctags
sudo apt-get install cscope

(2)taglist是vim的一个源代码浏览插件,不需要安装。只需要加入两个配置文件和在.vimrc中添加相应的配置即可。
可以到 http://www.vim.org/scripts/script.php?script_id=273上下载最新的taglist插件。插件中有两个文件,
一个taglist.vim,另一个是taglist.txt。

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

在.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
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>

 

3.使用cscope:


(1)在你需要浏览源码的根目录下使用下面命令:$cscope -Rbkq其中:(注意:进入vim之后要先输入cs add cscope.out,才可以输入命令搜索,如果嫌麻烦的话可以直接将cs add cscope.out添加到vimrc下。以后就可以直接查找了。)

   R 表示把所有子目录里的文件也建立索引
   b 表示cscope不启动自带的用户界面,而仅仅建立符号数据库
   q 生成cscope.in.out和cscope.po.out文件,加快cscope的索引速度
   k 在生成索引文件时,不搜索/usr/include目录

(2)常用的命令:

  :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 ---- 查找包含本文件的文件
Ctrl+]将跳到光标所在变量或函数的定义处 Ctrl+T返回

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

5.使用taglist:
 在taglist窗口中,可以使用下面的快捷键:

 <CR>          跳到光标下tag所定义的位置,用鼠标双击此tag功能也一样
 o             在一个新打开的窗口中显示光标下tag
 <Space>       显示光标下tag的原型定义
 u             更新taglist窗口中的tag
 s             更改排序方式,在按名字排序和按出现顺序排序间切换
 x             taglist窗口放大和缩小,方便查看较长的tag
 +             打开一个折叠,同zo
 -             将tag折叠起来,同zc
 *             打开所有的折叠,同zR
 =             将所有tag折叠起来,同zM
 [[            跳到前一个文件
 ]]            跳到后一个文件
 q             关闭taglist窗口
 <F1>          显示帮助

由于我们刚刚在.vimrc中添加了这么一句

nnoremap <silent> <F8> :TlistToggle<CR>
所以只要按f8就可以打开和关闭Tlist,效果如下:

注意:

  在使用cscope时,只能在cscope.out所在文件夹最开始使用vim,然后可以检索到子文件夹里面的东西


 

posted @ 2013-03-28 10:25  starlitnext  阅读(1618)  评论(0编辑  收藏  举报