Chinaunix首页 | 论坛 | 博客
  • 博客访问: 133394
  • 博文数量: 38
  • 博客积分: 1277
  • 博客等级: 中尉
  • 技术积分: 450
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-11 23:24
文章分类

全部博文(38)

文章存档

2012年(2)

2011年(7)

2010年(24)

2009年(5)

分类: LINUX

2011-07-21 09:24:29

ctags+taglist+cscope

1.taglist
注意:taglist依赖于ctags,所以要先装ctags,否则taglist装了也没法用!
(1)将taglist_45.zip解压得到的两个文件夹:doc和plugin 放到 ~/.vim文件夹里面,如果主目录下没有这个隐藏的文件夹,就建一个。
(2)
plugin/taglist.vim – taglist插件
doc/taglist.txt - taglist帮助文件
(3)常用快捷键
ctrl+ww    在列表和源文件中切换
ctrl + ]     查看函数定义
ctrl + o     返回上一级文件。
gf             查看头文件
gd        转到当前光标所指的局部变量的定义处。
*        转到当前光标所在单词的下一次出现的地方。
#        转到当前光标所在的单词的上一次出现的地方。
          跳到光标下tag所定义的位置,用鼠标双击此tag功能也一样  
o             在一个新打开的窗口中显示光标下tag  
       显示光标下tag的原型定义  
u             更新taglist窗口中的tag  
s             更改排序方式,在按名字排序和按出现顺序排序间切换 
x             taglist窗口放大和缩小,方便查看较长的tag   
+             打开一个折叠,同zoctags常用快捷键ctags常用快捷键  
-             将tag折叠起来,同zc  
*             打开所有的折叠,同zR  
=             将所有tag折叠起来,同zM  
[[            跳到前一个文件 
]]            跳到后一个文件
q             关闭taglist窗口 

2.ctags
(1)安装ctags : sudo apt-get install ctags
(2)使用
ctags -R      对当前目录下的文件递归产生tags文件。
ctags file.c
(3)在.vimrc文件中添加配置
:TlistToggle 或 :Tlist     显示函数列表
在.vimrc文件中加上键盘映射
map :Tlist
这样我就可以使用快捷键F8直接进入tlist模式了。这里注意和 : 之间有空格的。
在.vimrc下加入一下两行
set tags=tags;
set autochdir
这样使用tags时可以首先在当前目录下查找tags文件,如果没有则转到父目录查找。依次向上。

查看帮助文件
:help helptags
:help taglist.txt

3.cscope
(1)仔细阅读了cscope的手册后发现,原来cscope在产生索引文件时,只搜索类型为 C, lex和yacc的文件(后缀名为.c, .h, .l, .y),
C++的文件根本没有生成索引。不过按照手册上的说明,cscope支持c++和Java语言的文件。
于是按照cscope手册上提供的方法,先产生一个文件列表,然后让cscope为这个列表中的每个文件都生成索引。
为了方便使用,编写了下面的脚本来更新cscope和ctags的索引文件:

 #!/bin/sh 
find  . -name "*.h" -o -name "*.c" -o -name "*.cc" > cscope.files 
cscope  -bkq -i cscope.files 
ctags  -R    
 
 -R : 在生成索引文件时,搜索子目录树中的代码
 -b : 只生成索引文件,不进入cscope的界面
 -k : 在生成索引文件时,不搜索 / usr / include目录
 -q : 生成cscope . in . out和cscope . po . out文件,加快cscope的索引速度
将这个脚本保存到/usr/local/bin/cscopes.sh,以后就可以在源代码目录下使用sudo bash cscope.sh命令直接生成索引拉!
(2)cscope常用快捷键:
先按 ctrl+\ 然后 
c: 查找该函数被调用的位置 
d: 查找该函数调用了哪些函数 
e: 查找指定的正规表达式 f: 查找指定的文件 
g: 查找指定标识符的定义位置 
i: 查找该文件在哪些地方被包含 
s: 查找指定标识符的使用位置 
t: 查找指定的文本字符串 

需要上面的快捷键的话,还需要修改/etc/vimrc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CSCOPE settings for vim          
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
" This file contains some boilerplate settings for vim's cscope interface,
" plus some keyboard mappings that I've found useful.
"
" USAGE:
" -- vim 6:     Stick this file in your ~/.vim/plugin directory (or in a
"               'plugin' directory in some other directory that is in your
"               'runtimepath'.
"
" -- vim 5:     Stick this file somewhere and 'source cscope.vim' it from
"               your ~/.vimrc file (or cut and paste it into your .vimrc).
"
" NOTE:
" These key maps use multiple keystrokes (2 or 3 keys).  If you find that vim
" keeps timing you out before you can complete them, try changing your timeout
" settings, as explained below.
"
" Happy cscoping,
"
" Jason Duell       jduell@alumni.princeton.edu     2002/3/7
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


" This tests to see if vim was configured with the '--enable-cscope' option
" when it was compiled.  If it wasn't, time to recompile vim...
if has("cscope")

    """"""""""""" Standard cscope/vim boilerplate

    " use both cscope and ctag for 'ctrl-]', ':ta', and 'vim -t'
    set cscopetag

    " check cscope for definition of a symbol before checking ctags: set to 1
    " if you want the reverse search order.
    set csto=0

    " 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

    " show msg when any other cscope db added
    set cscopeverbose 


    """"""""""""" 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
    "
    " Below are three sets of the maps: one set that just jumps to your
    " search result, one that splits the existing vim window horizontally and
    " diplays your search result in the new window, and one that does the same
    " thing, but does a vertical split instead (vim 6 only).
    "
    " I've used CTRL-\ and CTRL-@ as the starting keys for these maps, as it's
    " unlikely that you need their default mappings (CTRL-\'s default use is
    " as part of CTRL-\ CTRL-N typemap, which basically just does the same
    " thing as hitting 'escape': CTRL-@ doesn't seem to have any default use).
    " If you don't like using 'CTRL-@' or CTRL-\, , you can change some or all
    " of these maps to use other keys.  One likely candidate is 'CTRL-_'
    " (which also maps to CTRL-/, which is easier to type).  By default it is
    " used to switch between Hebrew and English keyboard mode.
    "
    " All of the maps involving the macro use '^$': this is so
    " that searches over '#include " return only references to
    " 'time.h', and not 'sys/time.h', etc. (by default cscope will return all
    " files that contain 'time.h' as part of their name).


    " 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 s :cs find s =expand("")   
    nmap g :cs find g =expand("")   
    nmap c :cs find c =expand("")   
    nmap t :cs find t =expand("")   
    nmap e :cs find e =expand("")   
    nmap f :cs find f =expand("")   
    nmap i :cs find i ^=expand("")$
    nmap d :cs find d =expand("")   


    " Using 'CTRL-spacebar' (intepreted as CTRL-@ by vim) then a search type
    " makes the vim window split horizontally, with search result displayed in
    " the new window.
    "
    " (Note: earlier versions of vim may not have the :scs command, but it
    " can be simulated roughly via:
    "    nmap s :cs find s =expand("")   

    nmap s :scs find s =expand("")   
    nmap g :scs find g =expand("")   
    nmap c :scs find c =expand("")   
    nmap t :scs find t =expand("")   
    nmap e :scs find e =expand("")   
    nmap f :scs find f =expand("")   
    nmap i :scs find i ^=expand("")$   
    nmap d :scs find d =expand("")   


    " Hitting CTRL-space *twice* before the search type does a vertical
    " split instead of a horizontal one (vim 6 and up only)
    "
    " (Note: you may wish to put a 'set splitright' in your .vimrc
    " if you prefer the new window on the right instead of the left

    nmap s :vert scs find s =expand("")
    nmap g :vert scs find g =expand("")
    nmap c :vert scs find c =expand("")
    nmap t :vert scs find t =expand("")
    nmap e :vert scs find e =expand("")
    nmap f :vert scs find f =expand("")   
    nmap i :vert scs find i ^=expand("")$   
    nmap d :vert scs find d =expand("")


    """"""""""""" key map timeouts
    "
    " By default Vim will only wait 1 second for each keystroke in a mapping.
    " You may find that too short with the above typemaps.  If so, you should
    " either turn off mapping timeouts via 'notimeout'.
    "
    "set notimeout
    "
    " Or, you can keep timeouts, by uncommenting the timeoutlen line below,
    " with your own personal favorite value (in milliseconds):
    "
    "set timeoutlen=4000
    "
    " Either way, since mapping timeout settings by default also set the
    " timeouts for multicharacter 'keys codes' (like ), you should also
    " set ttimeout and ttimeoutlen: otherwise, you will experience strange
    " delays as vim waits for a keystroke after you hit ESC (it will be
    " waiting to see if the ESC is actually part of a key code like ).
    "
    "set ttimeout
    "
    " personally, I find a tenth of a second to work well for key code
    " timeouts. If you experience problems and have a slow terminal or network
    " connection, set it higher.  If you don't set ttimeoutlen, the value for
    " timeoutlent (default: 1000 = 1 second, which is sluggish) is used.
    "
    "set ttimeoutlen=100

endif
阅读(669) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~