安装ctags和cscope
sudo apt-get install ctags
sudo apt-get install cscope
在/home/username/下建立一个文件夹 .vim/plugins 或 .vim/plugin(vim的版本不同,目录不同,一般是在.vim/plugin下),注意:是在用户的home目录下进行创建,而且是隐藏文件夹。
在.vim/plugin目录下创建以下两个文件
cs_search_string.vim
"cs search word
func CS_Search_Word()
let w = expand("<cword>")
exe "cs f s" w
exe "copen"
endfunc
" find the define
func CS_Search_define()
let w = expand("<cword>")
exe "cs f g" w
exe "copen"
endfunc
" find call what funcs
func CS_Search_whatfunc()
let w = expand("<cword>")
exe "cs f d" w
endfunc
" find called func
func CS_Search_calledfunc()
let w = expand("<cword>")
exe "cs f c" w
endfunc
" find where file the func is
func CS_Search_funcfiles()
let w = expand("<cword>")
exe "cs f i" w
endfunc
search_word.vim
"search word
func Search_Word()
let w = expand("<cword>")
exe "vimgrep" w "./**/*.c ./**/*.h"
exe "copen"
endfunc
在用户自己的home目录下创建自己的.vimrc文件。注意,以下文件是我的配置,你可以修改成你的配置,我在相关的映射上都加上了注释。该文件的内容如下:
set nu
set tabstop=4
set et
set fo-=r
set noautoindent
syntax on
" 把光标放到一个函数上,按f5就可以打开该函数的定义窗口
map <F5> <C-w>]
" 按f6关闭定义窗口
map <F6> <C-w>c
" 按f4可以查看括号的匹配
map <F4> <S-%>
" 在插入模式下输入zz,即可退出编辑状态
imap zz <Esc>
set sw=4
"map <F2> :s/$/\= strftime(" %Y-%m-%d %H:%M:%S")<Enter>
" 在光标所在行的末尾插入日期,这个可以按自己的需要去留
map <F2> :s/$/\= strftime(" %Y-%m-%d")<Enter>
" 在项目级别查找单词所在的地方,并弹出窗口
map <F9> :call Search_Word()<CR>
" 按<查看前一项内容
map < :cprevious<CR>
" 按>查看后一项内容
map > :cnext<CR>
" 只是查看函数被调用的地方,而不是字符串搜素
map <F7> :call CS_Search_Word()<CR>
" 关闭弹出窗口
map <F8> :cclose<CR>
if has("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 ./cscope.out
" else add database pointed to by environment
elseif $CSCOPE_DB != ""
"if $CSCOPE_DB != ""
cs add $CSCOPE_DB
endif
set csverb
endif
set cscopequickfix=e-,i-,s-,t-
" 查看函数被调用的地方
map <F11> :call CS_Search_calledfunc()<CR>
" 查看函数定义
map <F12> :call CS_Search_whatfunc()<CR>
hi Comment ctermfg=6
set nocompatible
"filetype indent on
"set autoindent
"set ic
set hls
"set lbr
colorscheme delek
创建建立索引命令
脚本ctcs创建完成后,把脚本放到/usr/local/bin/下即可。
#!/bin/bash
#
# ctcs
star()
{
ctags -R *
if [ $? -eq 0 ]; then
echo "ctags successfully!"
fi
cscope -qbR
if [ $? -eq 0 ]; then
echo "cscope successfully!"
fi
}
del()
{
if [ -f tags -o -f cscope.out -o -f cscope.po.out -o -f cscope.in.out ]; then
rm -f tags && echo "clean tags ok!"
rm -f cscope.* && echo "clean cscope.* files ok!"
fi
}
case "$1" in
-r)
del
star
;;
-d)
del
;;
*)
echo "usage : ctcs -r|-d"
exit 1
esac
exit 0
安装GNU global
sudo apt-get install global
配置~/.vimrc
添加如下信息
"gtags 设置项
set cscopetag " 使用 cscope 作为 tags 命令
set cscopeprg='gtags-cscope' " 使用 gtags-cscope 代替 cscope
let GtagsCscope_Auto_Load = 1
let CtagsCscope_Auto_Map = 1
let GtagsCscope_Quiet = 1
let gtags_file=findfile("GTAGS", ";") "查找 gtags 文件
if !empty(gtags_file)
exe "cs add" gtags_file
endif
使用方法
在想要使用gtags的源码根目录执行
gtags
vim打开源码根目录下任意源码文件
Ctrl+} 跳转到函数定义处
Ctrl+t 跳转回来