ubuntu vim+ctags+taglist+cscope

Vi大家都很熟悉,某些情况下它是我们在Linux下唯一可用的文本编辑器。其实我们平时所指的VI其实是VIM(ViImproved,VI增强版)。有人常常拿VIM和emacs比较,VIM尽管不象emacs那样有那么多的功能,但是比emacs更方便、好用。VIM简单易用,只要看看VIM自带的vitutor,大约只要20分钟就能掌握VIM的用法。对于Linux下的C/C++的程序员,使用VIM+Ctags的组合来写程序也许是最佳的选择。^_^

VI的使用方法这里就不罗嗦了,本文假设大家已花了20分钟学习过了vitutor。 ^_^
        本节我们主要讲一下Ctags这个程序。尽管ctags也可以支持其它编辑器,但是它正式支持的只有VIM。并且VIM中已经默认安装了Ctags,它可以帮助程序员很容易地浏览源代码。
熟练的使用ctags仅需记住下面七条命令:(很简单吧,呵呵)
        1. $ ctags –R *      ($ 为Linux系统Shell提示符)
        2.  $ vi –t tag         (请把tag替换为您欲查找的变量或函数名)
        3. :ts                                (ts 助记字:tags list, “:”开头的命令为VI中命令行模式命令)
        4. :tp                                (tp 助记字:tags preview)---此命令不常用,可以不用记
        5. :tn                                (tn 助记字:tags next) ---此命令不常用,可以不用记
        6. Ctrl + ]
        7. Ctrl + T
        
下面我们逐条的解释上面的命令:
“$ ctags –R *”:“-R”表示递归创建,也就包括源代码根目录(当前目录)下的所有子目录。“*”表示所有文件。这条命令会在当前目录下产生一个“tags”文件,当用户在当前目录中运行vi时,会自动载入此tags文件。Tags文件中包括这些对象的列表:
用#define定义的宏
枚举型变量的值
函数的定义、原型和声明
名字空间(namespace)
类型定义(typedefs)
变量(包括定义和声明)
类(class)、结构(struct)、枚举类型(enum)和联合(union)
类、结构和联合中成员变量或函数
VIM用这个“tags”文件来定位上面这些做了标记的对象。

剩下的命令就是定位这些对象的方法:
“$ vi –t tag” :在运行vim的时候加上“-t”参数,例如:
[/usr/src]$ vim -t main
这个命令将打开定义“main”(变量或函数或其它)的文件,并把光标定位到这一行。如果这个变量或函数有多处定义,在VI命令行模式“:ts”命令就能列出一个列表供用户选择。    “:tp”为上一个tag标记文件,“:tn”为下一个tag标记文件。当然,若当前tags文件中用户所查找的变量或函数名只有一个,“:tp,:tn”命令不可用。 :(
最方便的方法是把光标移到变量名或函数名上,然后按下“Ctrl+]”,这样就能直接跳到这个变量或函数定义的源文件中,并把光标定位到这一行。用“Ctrl+t”可以退回原来的地方。即使用户使用了N次“Ctrl+]”查找了N个变量,按N次“Ctrl+t”也能回到最初打开的文件,它会按原路返回 。 ^_^

注意:运行vim的时候,必须在“tags”文件所在的目录下运行。否则,运行vim的时候还要用“:settags=”命令设定“tags”文件的路径,这样vim才能找到“tags”文件。在完成编码时,可以手工删掉tags文件(帚把不到,灰尘不会自己跑


在Ubuntu中vim的配置文件存放在/etc/vim目录中,配置文件名为Ubuntu vimrc


前提:已经安装了vim,建议安装vim-full,即:sudoapg-get install vim-full
   
    1.ctags:

    下载ctags,可从官网上下载,如:ctags-5.7.tag.gz

    tarxzvf ctags-5.7.tag.gz

    cdctags-5.7

    ./configure

    make

    sudomake install

    具体操作时目录可能不一样

    2.taglist:

    cd~

    mkdir.vim

    cd.vim

    mkdirplugin

    unzip(path自定)/taglist_45.zip

    ls

    //解压成功会在.vim下生成两个文件夹,doc, plugin

    测试一下插件是否安装好,可以运行一下命令

    1、ctags--version,有信息则说明安装好了

    2、进入一个原文件,vitest.c,进入命令模式,输入命令":Tlist",如果出现左右两栏分栏显示,则说明OK

    把ctags关联到taglist

    vi~/.vim/plugin/taglist.vim

    在命令行模式下输入命令查抄":/loaded_taglist"

    找到后,在该行前面加上如下行:

    letTlist_Ctags_Cmd="/usr/bin/ctags""目录可能有变,或许是/usr/local/bin/ctags

    OK

    到你想浏览的目录下

    ctags-R

    vitest.c

    :syntaxon //打开语法高亮

    :setcindent //打开自动缩进

    :Tlist//OK

3.cscope

sudo apt-get install cscope


安装好开发工具后,配置一下.vimrc文件在用户的主目录下:

我的.vimrc如下:

set hlsearch
set ai
syntax on
set number
set nocompatible
set columns=80
filetype on
set history=1000
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set showmatch
set guioptions-=T
set vb t_vb=
set ruler
set nohls
set noexpandtab
set ignorecase
set mouse=a
set cindent
set incsearch
set fencs=utf-8,cp936

set showcmd
"Tlist的内部变量。函数列表。
let Tlist_Use_Right_Window=1
let Tlist_File_Fold_Auto_Close=1
" "函数和变量列表
map <F4> :TlistToggle<CR>
" "全能补全
inoremap <F8> <C-x><C-o>
" " 没事,鼠标画线玩的。
noremap <F9> :call ToggleSketch()<CR>
set wildmenu
" " 启动函数变量快速浏览的时间设置
set updatetime=100
map <F2> :split<CR>
map <F3> :vsplit<CR>
map <F5> :q!<CR>
map <F6> :w!<CR>
map <F7> :wq!<CR>

autocmd FileType python set omnifunc=pythoncomplete#Complete


set hlsearch
set ai
syntax on
set number
set nocompatible
set columns=80
filetype on
set history=1000
set autoindent
set smartindent
set tabstop=4
set shiftwidth=4
set showmatch
set guioptions-=T
set vb t_vb=
set ruler
set nohls
set noexpandtab
set ignorecase
set mouse=a
set cindent
set incsearch
set fencs=utf-8,cp936

set showcmd
"Tlist的内部变量。函数列表。
let Tlist_Use_Right_Window=1
let Tlist_File_Fold_Auto_Close=1
" "函数和变量列表
map <F4> :TlistToggle<CR>
" "全能补全
inoremap <F8> <C-x><C-o>
" " 没事,鼠标画线玩的。
noremap <F9> :call ToggleSketch()<CR>
set wildmenu
" " 启动函数变量快速浏览的时间设置
set updatetime=100
map <F2> :split<CR>
map <F3> :vsplit<CR>
map <F5> :q!<CR>
map <F6> :w!<CR>
map <F7> :wq!<CR>


autocmd FileType python set omnifunc=pythoncomplete#Complete




""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 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 <cfile> macro use '^<cfile>$': this is so
    " that searches over '#include <time.h>" 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 <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>    


    " 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 <C-@>s <C-W><C-S> :cs find s <C-R>=expand("<cword>")<CR><CR>    

    nmap <C-@>s :scs find s <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>g :scs find g <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>c :scs find c <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>t :scs find t <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>e :scs find e <C-R>=expand("<cword>")<CR><CR>    
    nmap <C-@>f :scs find f <C-R>=expand("<cfile>")<CR><CR>    
    nmap <C-@>i :scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>    
    nmap <C-@>d :scs find d <C-R>=expand("<cword>")<CR><CR>    


    " 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 <C-@><C-@>s :vert scs find s <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>g :vert scs find g <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>c :vert scs find c <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>t :vert scs find t <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>e :vert scs find e <C-R>=expand("<cword>")<CR><CR>
    nmap <C-@><C-@>f :vert scs find f <C-R>=expand("<cfile>")<CR><CR>    
    nmap <C-@><C-@>i :vert scs find i ^<C-R>=expand("<cfile>")<CR>$<CR>    
    nmap <C-@><C-@>d :vert scs find d <C-R>=expand("<cword>")<CR><CR>


    """"""""""""" 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 <F1>), 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 <F1>).
    "
    "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
    

注:
“U”命令自己改变自己,“u”命令撤销操作,CTRL-R命令重做操作。这有点乱,但不用
担心,用“u”和CTRL-R命令你可以切换到任何状态。


流行的文本编辑器通常都有前进和后退功能,可以在文件中曾经浏览过的位置之间来回移动。在 vim 中使用 Ctrl-O 执行后退,使用 Ctrl-I 执行前进。


 cscope -Rbq    Ctrl+d退出cscope

在Ubuntu中vim的配置文件存放在/etc/vim目录中,配置文件名为Ubuntu vimrc

vim d 删除行  x删除字符  gf转到头文件 Ctrl+k系统函数帮助,或者K,gd 转到变量定义。

从别的编辑器里粘贴到vim里的代码经常由于不正常的缩进变得格式混乱。

格式化全文: gg=G (注意不要在命令行里输入)

自动缩进当前行: ==

:help gg
:help =
:help G
:help ‘formatprg’
:help C-indenting


ctrl +p  代码补全。

vim  gnome 



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值