VIM: C程序员插件C.vim

作为一个程序员,在编码时你常常有如下的重复操作:
  1. 给文件或者函数说明
  2. 给函数添加注释,或者添加一个注释快
  3. 添加一段已有代码 (:r !cat filename)
  4. 检查代码语法
  5. 读函数的帮助文档
  6. 注释掉一块代码,将"//"转换为/* */
 
  C.vim即C-Support vim,能够帮助C程序员[当然还有使用C++ 的家伙]很好的完成上述工作,为你节约时间,并保护你的键盘。插件作者Fritz Mehner,编写c.vim的宗旨是“Write and run programs. Insert statements, idioms, comments”.
   本文就如何安装C.vim插件和它的基本功能做一个概述:
 
  安装C.vim插件三步走:
  第一步:
    下载插件vim.org [最好单击该链接下载,wget有可能不好使哦]
 
  1. $ cd /usr/src 
  2. $ wget http://www.vim.org/scripts/download_script.php?src_id=9679 
$ cd /usr/src
$ wget http://www.vim.org/scripts/download_script.php?src_id=9679
  第二步:
    安装插件了,VIM插件不过是一些脚本而已,调用即可
  1. $ mkdir ~/.vim 
  2. $ cd ~/.vim 
  3. $ unzip /usr/src/cvim.zip 
$ mkdir ~/.vim
$ cd ~/.vim
$ unzip /usr/src/cvim.zip
 
  第三步:

    激活插件功能

  1. $ vim ~/.vimrc 
  2. filetype plugin on 
$ vim ~/.vimrc
filetype plugin on
  

   八项强大的功能

功能一:自动为*.c文件添加文件头说明

       当你打开一个扩展名为.c的文件,在文件开始处会自动加上如下的描述,并且将光标定位到description处,此时是insert mode可直接键入文件描述
  1. $ vim myprogram.c 
  2. /*
  3. * =================================================
  4. *       Filename:  myprogram.c
  5. *
  6. *    Description:
  7. *
  8. *        Version:  1.0
  9. *        Created:  01/19/09 20:23:25
  10. *       Revision:  none
  11. *       Compiler:  gcc
  12. *
  13. *         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
  14. *        Company:  FH Südwestfalen, Iserlohn
  15. *
  16. * =================================================
  17. */ 
$ vim myprogram.c
/*
* =================================================
*       Filename:  myprogram.c
*
*    Description:
*
*        Version:  1.0
*        Created:  01/19/09 20:23:25
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  Dr. Fritz Mehner (mn), mehner@fh-swf.de
*        Company:  FH Südwestfalen, Iserlohn
*
* =================================================
*/
     

        当然你可以改变AUTHOR COMPANY等得默认值。

  1. $ vim ~/.vim/c-support/templates/Templates 
  2. |AUTHOR|    = geekstuff 
  3. |AUTHORREF| = gk 
  4. |EMAIL|     = subscribe@geekstuff 
  5. |COMPANY|   = thegeekstuff.com 
$ vim ~/.vim/c-support/templates/Templates
|AUTHOR|    = geekstuff
|AUTHORREF| = gk
|EMAIL|     = subscribe@geekstuff
|COMPANY|   = thegeekstuff.com
      

        现在再创建一个.c 文件,你会看到如下的文件头。

  1. $ vim myprogram.c 
  2. /*
  3. * =================================================
  4. *
  5. *       Filename:  myprogram.c
  6. *
  7. *    Description:
  8. *
  9. *        Version:  1.0
  10. *        Created:  01/19/09 20:26:43
  11. *       Revision:  none
  12. *       Compiler:  gcc
  13. *
  14. *         Author:  geekstuff (gk), subscribe@geekstuff
  15. *        Company:  thegeekstuff.com
  16. *
  17. * =================================================
  18. */ 
$ vim myprogram.c
/*
* =================================================
*
*       Filename:  myprogram.c
*
*    Description:
*
*        Version:  1.0
*        Created:  01/19/09 20:26:43
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  geekstuff (gk), subscribe@geekstuff
*        Company:  thegeekstuff.com
*
* =================================================
*/

功能二:插入一个函数  \if

        当需要创建一个新函数时,只要在正常模式(ESC)键入\if,VIM会提示键入函数名(如图一),会直接得到图二的默认函数内容。

图1 自动插入函数,键入函数名


图二 自动插入函数效果


    功能三:插入main函数 \im

        与功能二类似,在正常模式下键入\im, 得到图三的默认的main函数内容

图三 自动插入main函数效果

       
    功能四: 插入函数头 \cfu

        插入一个函数头说明,在正常模式下键入\cfu,VIM会提示键入函数名,如图四,会得到如图五的函数说明注释块。

图四 插入函数头,键入函数名


       

图五 插入函数头效果

    功能五: 添加一个注释块 \cfr
        添加一个注释块, 在正常模式下键入\cfr, 会得到图6的注释块,并且光标被定位到注释块内,直接可键入注释。
       
图六 插入注释块效果

功能六:包含(include)一个头文件 \p<

     在正常模式下键入\p<, 会得到“#include <>”,并且光标被定位到尖括号内,可以直接键入要包含的头文件名


功能七: 保存文件,编译后立即执行

        保存后编译, \rc
        运行 \\r
       
功能八: 插入预先编辑好的程序块 \nr
       在安装插件时,插件自带了一些编译好的代码,你可以把这些代码插入你的程序中。       
  1. $ ls ~/.vim/c-support/codesnippets 
  2. Makefile                        calloc_double_matrix.c  main.c   print_double_array.c.noindent 
  3. Makefile.multi-target.template  calloc_int_matrix.c     main.cc  print_int_array.c.noindent 
$ ls ~/.vim/c-support/codesnippets
Makefile                        calloc_double_matrix.c  main.c   print_double_array.c.noindent
Makefile.multi-target.template  calloc_int_matrix.c     main.cc  print_int_array.c.noindent

        译者注: 这个功能与 :r !cat filename 完成的功能相似,这里只是有一个默认路径,还可以保存,下面这段很不错的
    你也可以自己编写一些常用模板代码放在~/.vim/c-support/codesnippets/. 当然在你读代码的时候也可以保存某些代码片段, 方法: 选中想要保存的代码片段,键入\nw,给它取一个名字。下次你就可以用过\nr使用这段代码了


原文:Tutorial: Make Vim as Your C/C++ IDE Using c.vim Plugin


    更多参考: 

       README : ~/.vim/README.csupport
        PDF : ~/.vim/c-support/doc/c-hotkeys.pdf
        Online c-support vim plugin documentation
        Additional Screenshots of this plug-in.

        VIM Plugins for C / C++ developers

 

 

Cvim HotKey:

 

-- 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)
  \cfdi     file description (implementation)   (n,i)
  \cfdh     file description (header)           (n,i)
  \ccs      C/C++-file section  (tab. compl.)   (n,i)
  \chs      H-file section      (tab. compl.)   (n,i)
  \ckc      keyword comment     (tab. compl.)   (n,i)
  \csc      special comment     (tab. compl.)   (n,i)
  \cd       date                                (n,v,i)
  \ct       date \& time                        (n,v,i)


  -- Statements ---------------------------------------------------------


  \sd       do { } while                        (n,v,i)
  \sf       for                                 (n,i)
  \sfo      for { }                             (n,v,i)
  \si       if                                  (n,i)
  \sif      if { }                              (n,v,i)
  \sie      if else                             (n,v,i)
  \sife     if { } else { }                     (n,v,i)
  \se       else { }                            (n,v,i)
  \sw       while                               (n,i)
  \swh      while { }                           (n,v,i)
  \ss       switch                              (n,v,i)
  \sc       case                                (n,i)
  \s{ \sb   { }                                 (n,v,i)


  -- Preprocessor -------------------------------------------------------


  \ps       choose a standard library include   (n,i)
  \pc       choose a C99 include                (n,i)
  \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)
  \+"       << ""                               (n,i)
  \+c       class                               (n,i)
  \+ps      #include <...> STL                  (n,i)
  \+pc      #include <c..> C                    (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)
  \rmc      run 'make clean'                    (n,i)
  \rme      executable to run                   (n,i)
  \rma      cmd. line arg. for make             (n,i)
  \rp       run splint                          (n,i)
  \rpa      cmd. line arg. for splint           (n,i)
  \rk       run CodeCheck (TM)                  (n,i)
  \rka      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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值