Vi经典插件ctags(转)

来源:http://www.cnblogs.com/lihaozy/archive/2012/08/06/2624909.html

(为了提高工作效率,必须学会使用一些工具)


. 查看 key 相关信息说明的命令 :help keycodes

==============================================================================================================
2. ctags
(1). 帮助手册查看
:help usr_29

(2). 功能
ctags的功能, 只要在unix/linux环境编程过的人都知道它的历害! 我在这也作个小小介绍吧: 对浏览代码非常的方便, 可以在函数, 变量之间跳来跳去等等.(注意: 我这里说到的仅是小小的一部分!).

(3). 安装
首先, 下载ctags安装包, 然后解压并安装, 命令如下:
$ tar -xzvf ctags-5.6.tar.gz
$ cd ctags-5.6
$ make
# make install   // 需要root权限 
        或:
        $ tar -xzvf ctags-5.7.tar.gz
        $ cd ctags-5.7
        $ ./configure
        # make
        # make install


(4). 使用方法
然后去你的源码目录, 如果你的源码是多层的目录, 就去最上层的目录, 在该目录下运行命令: ctags -R
我现在以 vim71 的源码目录做演示
$ cd /home/nuoerll/vim71
$ ctags -R

此时在/home/nuoerll/vim71目录下会生成一个 tags 文件, 现在用vim打开 /home/nuoerll/vim71/src/main.c
$ vim /home/nuoerll/vim71/src/main.c

再在vim中运行命令:
:set tags=/home/nuoerll/vim71/tags
该命令将tags文件加入到vim中来, 你也可以将这句话放~/.vimrc中去, 如果你经常在这个工程编程的话.
对于经常在不同工程间编程, 可以在.vimrc中设置:
set tags=tags;    // ; 不能没有
set autochdir

(5). 使用例子
把光标定位到某一函数名上, 按下 Ctar + ], vim就可以自动切换到该函数定义处! 要返回只需要按下Ctrl + t .
更多用法, 在vim命令模式输入 :help usr_29 查看即可.


==============================================================================================================
3. TagList 插件
(1). 帮助手册查看
:help taglist.txt

(2). 功能
高效地浏览源码, 其功能就像vc中的workpace, 那里面列出了当前文件中的所有宏, 全局变量, 函数名等.

(3). 安装
下载taglist压缩包, 然后把解压的两个文件taglist.vim 和 taglist.txt 分别放到 $HOME/.vim/plugin 和 $HOME/.vim/doc 目录中.

(4). 使用方法
首先请先在你的~/.vimrc文件中添加下面语句:
let Tlist_Ctags_Cmd='/bin/ctags'   // 若在windows中应写成: let Tlist_Ctags_Cmd='ctags.exe'
let Tlist_Show_One_File=1
let Tlist_OnlyWindow=1
let Tlist_Use_Right_Window=0
let Tlist_Sort_Type='name'
let Tlist_Exit_OnlyWindow=1
let Tlist_Show_Menu=1
let Tlist_Max_Submenu_Items=10
let Tlist_Max_Tag_length=20
let Tlist_Use_SingleClick=0
let Tlist_Auto_Open=0
let Tlist_Close_On_Select=0
let Tlist_File_Fold_Auto_Close=1
let Tlist_GainFocus_On_ToggleOpen=0
let Tlist_Process_File_Always=1
let Tlist_WinHeight=10
let Tlist_WinWidth=18
let Tlist_Use_Horiz_Window=0


此时用vim打开一个c源文件试试:
$ vim ~/vim/src/main.c

进入vim后用下面的命令打开taglist窗口.
:Tlist

为了更方便地使用, 可以在.vimrc文件中加入:
map <silent> <leader>tl :TlistToggle<CR> 
这样就可以用 ",tl" 命令进行taglist窗口的打开和关闭之间方便切换了. // 这里的","是我.vimrc设置的leader, 你也可以设置成别的, 在.vimrc中修改即可, 如我的: let mapleader=","


==============================================================================================================
4. WinManager 插件
(1). 帮助手册查看
:help winmanager

(2). 功能
管理各个窗口, 或者说整合各个窗口.

(3). 安装
下载WinManager.zip压缩包, 解压后把*.vim文件放到 $HOME/.vim/plugin 目录中, 把*.txt文件放到 $HOME/.vim/doc 目录中.

(4). 使用方法
在.vimrc中加入如下设置:
let g:winManagerWindowLayout='FileExplorer|BufExplorer' // 这里可以设置为多个窗口, 如'FileExplorer|BufExplorer|TagList'
let g:persistentBehaviour=0      // 只剩一个窗口时, 退出vim.
let g:winManagerWidth=20
let g:defaultExplorer=1
nmap <silent> <leader>fir :FirstExplorerWindow<cr>
nmap <silent> <leader>bot :BottomExplorerWindow<cr>
nmap <silent> <leader>wm :WMToggle<cr>

(5). 使用例子
在终端输入vim启动vim:
$vim
在正常模式下, 敲入 ",wm" 即可看到, vim的左侧新建了两个窗口:FileExplorer和BufExplorer, 这样我们即可在FileExplorer窗口很方便地对目录进行查看, 编辑等操作; 在BufExplorer窗口中查看当前vim已经打开那些文件.


==============================================================================================================
5. cscope
(1). 帮助手册查看
:help if_cscop.txt

(2). 功能
用Cscope自己的话说 - "你可以把它当做是超过频的ctags", 其功能和强大程度可见一斑吧, 关于它的介绍我就不详细说了, 如果你安装好了前文介绍的帮助手册.

(3). 安装
    如果是在linux环境中, cscope一般都会随系统一起安装了; 在windows环境中, 则需要下载windows版本的(cscope.exe), 然后把它放到path环境变量所设
    置的目录中(如: C:\Program Files\Vim\vim72).

(4). 使用方法
    在.vimrc中增加如下设置, 就可以利用相应的快捷键进行不同的查找了.
        if has("cscope")
            set cscopetag   " 使支持用 Ctrl+] 和 Ctrl+t 快捷键在代码间跳来跳去
            " check cscope for definition of a symbol before checking ctags: set to 1
            " if you want the reverse search order.
            set csto=1

            " 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

            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>
        endif

(5). 使用例子
    首先进入源码目录, 在linux终端中输入以下命令以创建cscope数据库:
        $ find ~/work/..Project/ -name "*.h" -o -name "*.cpp" > cscope.files
        $ cscope -bkq -i cscope.files
    如果是在windows环境中, 则换成如下命令:
        dir /s /b *.cpp *.h > cscope.files
        cscope -b -q -k -i cscope.files

    然后, 用vim打开一个源文件(如: vim main.cpp),
    打开后, 第一件事就是导入cscope数据库了:
        :cs add /home/yourname/workpace/cscope.out /home/yourname/workpace

    cscope数据库导入成功后, 就可以利用"步骤(4)"中定义的快捷键进行相关的查找, 浏览等操作了(当然也可以直接利用相关命令, 嘿嘿).
                
==============================================================================================================
5. c.vim 插件
(1). 帮助手册查看
help csupport

(2). 功能
C/C++-IDE for Vim/gVim. 简单的说, 就是如果安装配置好后, vim/gvim就是一个c/c++编程的一个IDE, 其功能堪比windows里常用的vc.

(3). 安装
下载cvim.zip压缩包后, 把压缩包copy到 $HOME/.vim 目录(windows下, copy 到 C:\Program Files\Vim\vimfiles)下解压, 即可.
unzip cvim.zip    // 解压命令

(4). 使用方法
安装好后就可以直接用了, 具体例子看"使用例子".

(5). 使用例子
在终端用vim打开一个c文件:
$vim hello.c
进入vim, 敲入 "\im" 即可发现一个main函数框架就这样快捷简单完美地写出.

我比效常用的的操作有(第一列为命令, 第二列为说明, 第三列为该命令所支持的模式(n:普通模式, v:可视模式, i:插入模式):
    -- Help ---------------------------------------------------------------
    \hm       show manual for word under the cursor (n,i)
    \hp       show plugin help                      (n,i)

    -- Comments -----------------------------------------------------------
    \cl       end-of-line comment                 (n,v,i)
    \cj       adjust end-of-line comment(s)       (n,v,i)
    \cs       set end-of-line comment column      (n)
    \c*       code -> comment /* */               (n,v)
    \cc       code -> comment //                  (n,v)
    \co       comment -> code                     (n,v)
    \cfr      frame comment                       (n,i)
    \cfu      function comment                    (n,i)
    \cme      method description                  (n,i)
    \ccl      class description                   (n,i)
    \cd       date                                (n,v,i)
    \ct       date \& time                        (n,v,i)

    -- Statements ---------------------------------------------------------
    \sd       do { } while                        (n,v,i)
    \sfo      for { }                             (n,v,i)
    \sife     if { } else { }                     (n,v,i)
    \se       else { }                            (n,v,i)
    \swh      while { }                           (n,v,i)
    \ss       switch                              (n,v,i)

    -- Preprocessor -------------------------------------------------------
    \p<       #include <>                         (n,i)
    \p"       #include ""                         (n,i)
    \pd       #define                             (n,i)
    \pu       #undef                              (n,i)
    \pie      #if #else #endif                   (n,v,i)
    \pid      #ifdef #else #endif                 (n,v,i)
    \pin      #ifndef #else #endif                (n,v,i)
    \pind     #ifndef #def #endif                 (n,v,i)
    \pi0      #if 0 #endif                        (n,v,i)
    \pr0      remove #if 0 #endif                 (n,i)
    \pe       #error                              (n,i)
    \pl       #line                               (n,i)
    \pp       #pragma                             (n,i)

    -- Idioms -------------------------------------------------------------
    \if       function                            (n,v,i)
    \isf      static function                     (n,v,i)
    \im       main()                              (n,v,i)
    \i0       for( x=0; x<n; x+=1 )               (n,v,i)
    \in       for( x=n-1; x>=0; x-=1 )            (n,v,i)
    \ie       enum   + typedef                    (n,i)
    \is       struct + typedef                    (n,i)
    \iu       union + typedef                    (n,i)
    \ip       printf()                            (n,i)
    \isc      scanf()                             (n,i)
    \ica      p=calloc()                          (n,i)
    \ima      p=malloc()                          (n,i)
    \isi      sizeof()                            (n,v,i)
    \ias      assert()                            (n,v)
    \ii       open input file                     (n,i)
    \io       open output file                    (n,i)

    -- Snippets -----------------------------------------------------------
    \nr       read code snippet                   (n,i)
    \nw       write code snippet                  (n,v,i)
    \ne       edit code snippet                   (n,i)
    \np       pick up prototype                   (n,v,i)
    \ni       insert prototype(s)                 (n,i)
    \nc       clear prototype(s)                 (n,i)
    \ns       show   prototype(s)                 (n,i)
    \ntl      edit local templates                (n,i)
    \ntg      edit global templates               (n,i)
    \ntr      rebuild templates                   (n,i)

    -- C++ ----------------------------------------------------------------
    \+co      cout << << endl;                  (n,i)
    \+c       class                               (n,i)
    \+cn      class (using new)                   (n,i)
    \+ci      class implementation                (n,i)
    \+cni     class (using new) implementation    (n,i)
    \+mi      method implementation               (n,i)
    \+ai      accessor implementation             (n,i)

    \+tc      template class                      (n,i)
    \+tcn     template class (using new)          (n,i)
    \+tci     template class implementation       (n,i)
    \+tcni    template class (using new) impl.    (n,i)
    \+tmi     template method implementation      (n,i)
    \+tai     template accessor implementation    (n,i)

    \+tf      template function                   (n,i)
    \+ec      error class                         (n,i)
    \+tr      try ... catch                       (n,v,i)
    \+ca      catch                               (n,v,i)
    \+c.      catch(...)                          (n,v,i)

    -- Run ----------------------------------------------------------------
    \rc       save and compile                    (n,i)
    \rl       link                                (n,i)
    \rr       run                                 (n,i)
    \ra       set comand line arguments           (n,i)
    \rm       run make                            (n,i)
    \rg       cmd. line arg. for make             (n,i)
    \rp       run splint                          (n,i)
    \ri       cmd. line arg. for splint           (n,i)
    \rk       run CodeCheck (TM)                  (n,i)
    \re       cmd. line arg. for CodeCheck (TM)   (n,i)
    \rd       run indent                          (n,v,i)
    \rh       hardcopy buffer                     (n,v,i)
    \rs       show plugin settings                (n,i)
    \rx       set xterm size                      (n, only Linux/UNIX & GUI)
    \ro       change output destination           (n,i)
    
关于此插件的更多功能和各种说明, 请查看帮助手册, help csupport.

   
==============================================================================================================
6. omnicppcoplete 插件
(1). 帮助手册查看
:help omnicppcoplete
(2). 功能
实现像vc那样的代码自动补全功能, 比如 this-><Ctrl+X><Ctrl+O> 后, 将出现一个提示框, 其中包含了this指针所有可以接收的函数或数据成员等.
(3). 安装
把下载下来的 omnicppcoplete-0.41.zip 压缩包copy到 $HOME/.vim/ (windows 复制到 C:\Program Files\Vim\vimfiles ), 然后解压, 即可.
(4). 使用方法
在.vimrc中添加以下两条语句:
        set nocp   "不兼容vi
        filetype plugin on   "开启文件类型识别功能
进入c++源码目录, 在终端执行命令 ctags -R --c++-kinds=+p --fields=+iaS --extra=+q . 
(5). 使用例子
编写c++代码时, 如要自动补全, 敲入 Ctrl+X Ctrl+O, 即可在出现的提示框中用 Ctrl+N 选择符合要求的.


==============================================================================================================
7. a.vim插件
(1). 帮助手册查看
这个插件没有帮助手册, 不过大可放心使用, 其提供的功能也不是很多, 就几条命令, 但是用起来真的是很方便.
(2). 功能
在 .h 和 .c/.cpp 文件中切换. (英文原句 "A few of quick commands to swtich between source files and header files quickly.")
(3). 安装
把下载到的a.vim插件放到 $HOME/.vim/plugin 目录下, 即可.
(4). 使用方法
只要在vim中输入以下命令即可完成相应的功能:
:A switches to the header file corresponding to the current file being edited (or vise versa) 
:AS splits and switches 
:AV vertical splits and switches 
:AT new tab and switches 
:AN cycles through matches 
:IH switches to file under cursor 
:IHS splits and switches 
:IHV vertical splits and switches 
:IHT new tab and switches 
:IHN cycles through matches 
<Leader>ih switches to file under cursor 
<Leader>is switches to the alternate file of file under cursor (e.g. on <foo.h> switches to foo.cpp) 
<Leader>ihn cycles through matches

==============================================================================================================
8. VisualMark.vim插件
(1). 帮助手册查看
这个插件没有帮助手册, 不过大可放心使用, 其提供的功能也不是很多, 就几条命令, 但是用起来真的是很方便.
(2). 功能
高亮书签.
(3). 安装
把下载好的VisualMark.vim插件放到 $HOME/.vim/plugin 目录下, 即可.
(4). 使用方法
只要在vim中执行以下命令即可完成相应的功能:
   1. For gvim, use "Ctrl + F2" to toggle a visual mark. 
       For both vim and gvim, use "mm" to toggle a visual mark. 
   2. Use "F2" to navigate through the visual marks forward in the file. 
   3. Use "Shift + F2" to navigate backwards.

==============================================================================================================
9. Mark.vim插件
(1). 帮助手册查看
这个插件没有帮助手册, 不过大可放心使用, 其提供的功能也不是很多, 就几条命令, 但是用起来真的是很方便.

(2). 功能
这个插件与vim中自带的'*'与'#'非常相像. 不同之处是: vim中的'*'与'#'命令只能同时高亮一个同类词组(或正则表达式的搜索结果), 而Mark.vim插件可以同时高亮多个.

(3). 安装
把下载好的Mark.vim插件放到 $HOME/.vim/plugin 目录中, 即可.

(4). 使用方法
\m   mark or unmark the word under (or before) the cursor 
\r   manually input a regular expression. 用于搜索. 
\n   clear this mark (i.e. the mark under the cursor), or clear all highlighted marks .
\*   把光标向前切换到当前被Mark的MarkWords中的下一个MarkWord.
\#   把光标向后切换到当前被Mark的MarkWords中的上一个MarkWord.
\/ 把光标向前切换到所有被Mark的MarkWords中的下一个MarkWord.
\? 把光标向后切换到所有被Mark的MarkWords中的上一个MarkWord.

说明: 这些命令中的 '\' 是 vim 中的 mapleader, 你也可以设置为别的: 如, 若要设置为 ',', 把下面这条语句加到.vimrc文件中, 即可, 
   let mapleader=","


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值