1. 程式人生 > >vim中瀏覽c++程式碼使用ctags和cscope進行函式跳轉時vimrc的配置

vim中瀏覽c++程式碼使用ctags和cscope進行函式跳轉時vimrc的配置

依賴:ctags、cscope

方法:

在~/.vimrc中新增下面的程式碼。之後執行vimrc(source ~/.vimrc)。 

以後瀏覽程式碼的時候按F9就可以在當前目錄生成相應的tags,之後按“ctrl和]” 即可在c++中找到函式的定義或者變數的定義:

map <F9>  :call GenerateCtags()<CR>
           
function! GenerateCtags()
    let dir = getcwd()
    if filereadable("tags")
        let tagsdeleted=delete("./"."tags")
        if(tagsdeleted!=0)
            echohl WarningMsg | echo "Fail to do tags! I cannot delete the tags" | echohl None
            return
        endif
    endif  
    if has("cscope")
        silent! execute "cs kill -1"
    endif  
    if filereadable("cscope.files")
        let csfilesdeleted=delete("./"."cscope.files")
        if(csfilesdeleted!=0)
            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.files" | echohl None
            return
        endif
    endif  
    if filereadable("cscope.out")
        let csoutdeleted=delete("./"."cscope.out")
        if(csoutdeleted!=0)
            echohl WarningMsg | echo "Fail to do cscope! I cannot delete the cscope.out" | echohl None
            return
        endif
    endif  
    if(executable('ctags'))
        silent! execute "!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q ."
    endif  
    if(executable('cscope') && has("cscope") )
        silent! execute "!find . -name '*.h' -o -name '*.c' -o -name '*.cc' -o -name '*.java' -o -name '*.cs' > cscope.files"
        silent! execute "!cscope -Rbq"
        execute "normal :"
        if filereadable("cscope.out")
            execute "cs add cscope.out"
        endif                                                                                                                
    endif  
endfunction