Chinaunix首页 | 论坛 | 博客
  • 博客访问: 214190
  • 博文数量: 127
  • 博客积分: 1998
  • 博客等级: 上尉
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 16:41
文章分类

全部博文(127)

文章存档

2014年(41)

2013年(1)

2012年(85)

分类: LINUX

2012-01-11 20:54:31

  • This article is part of the ongoing  Series. As a programmer, you may do lot of repetitive tasks while coding such as:
    • Adding file header
    • Adding function/frame comment
    • Including default code snippet
    • Performing syntax check
    • Reading documentation about a function
    • Converting a code block to comment, and vice versa


    The C-Support Vim Plugin offers easiest way to do all of the above, saving lot of time and keystrokes for C and C++ programmers.

    The plugin was written by Fritz Mehner, who explains the purpose of the plugin as: “Write and run programs. Insert statements, idioms, comments”.

    He also highlights following features:

    • Statement oriented editing of C / C++ programs
    • Speed up writing new code considerably.
    • Write code and comments with a professional appearance from the beginning.
    • Use code snippets


    This article explains how to install the plugin in 3 easy steps and 7 powerful features of the plugin.

    3 Steps to Install the C.Vim PluginStep 1: Download C Vim Plugin

    Download the plugin from  website.

    $ cd /usr/src $ wget 2: Install the C Vim Plugin$ mkdir ~/.vim $ cd ~/.vim $ unzip /usr/src/cvim.zipStep 3: Enable the plugin in the ~/.vimrc

    Add the following line to the ~/.vimrc to enable the plugin for Vim editor.

    $ vim ~/.vimrc filetype plugin on8 Powerful Features of C.Vim PluginFeature 1: Add Automatic Header to *.c file

    When you open a file with the extension .c it opens the file with header as shown below. This will also place the cursor in the Description field in Insert mode.

    $ 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 * * ================================================= */


    To change the default value of the AUTHOR and COMPANY, modify the default value in ~/.vim/c-support/templates/Templates

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


    Now, when you create a new c file, it will show the modified values for AUTHOR and COMPANY as shown below.

    $ 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 * * ================================================= */


    Note: To add custom fields to the header, modify the ~/.vim/c-support/templates/file-description.template file and add your own custom field.

    Feature 2: Adding C function using \if

    For writing a subroutine, type \if in normal mode, which will prompt for the function name (as shown in Fig1 below) and inserts the subroutine with default function content (as shown in Fig2 below).

    Vim C/C++ IDE - Adding C Function - 1

    Fig1:Insert C Function Automatically

    Vim C/C++ IDE - Adding C Function - 2

    Fig 2:Insert C Function Automatically

    Feature 3: Insert main Function Header using \im

    For inserting main function, type \im in normal mode, which will add the main function as shown below.

    Fig 3: Insert C main function automatically

    Feature 4: Insert a Function Header using \cfu

    For inserting a function header, type \cfu in normal mode, which will ask the function name as shown in Fig 4, and adds the comment as shown in Fig 5.

    Vim C/C++ IDE - Insert C Function Header - 1

    Fig 4: Insert C Function Header Automatically

    Vim C/C++ IDE - Insert C Function Header - 1

    Fig 5: Insert C Function Header Automatically

    Feature 5: Add a Frame comment using \cfr

    To add a frame comment, type \cfr in normal mode, which will give the following formatted comment.

    Vim C/C++ IDE - Insert Frame Comment

    Fig 6: Insert a Frame Comment Automatically

    Feature 6: To include header file, use \p<

    Type \p< in the normal mode, which will include the text “#include <>”, and places the cursor in the < symbol in Insert mode where you can type the header file name.

    Feature 7: Save the file, compile it and execute it immediately.

    To save and compile the file use \rc.

    To run use \rr.

    Feature 8: Insert pre-defined code-snippet to the C code using \nr

    The plugin comes with few pre-defined code snippets that you can insert into your code. Following are the default code snippets that comes with the plugin.

    $ 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

    For example, if you want to create a function that will Allocate a dynamic int-matrix of size rows*columns; return a pointer, you can re-use it from the existing code snippets. Following is the content of the calloc_int_matrix.c pre-defined code snippets.

    /* * === FUNCTION ====================================================================== * Name: calloc_int_matrix * Description: Allocate a dynamic int-matrix of size rows*columns; return a pointer. * ===================================================================================== */ int** calloc_int_matrix ( int rows, int columns ) { int i; int **m; m = calloc ( rows, sizeof(int*) ); /* allocate pointer array */ assert( m != NULL ); /* abort if allocation failed */ *m = calloc ( rows*columns, sizeof(int) ); /* allocate data array */ assert(*m != NULL ); /* abort if allocation failed */ for ( i=1; i m[i] = m[i-1] + columns; return m; } /* ---------- end of function calloc_int_matrix ---------- */


    To insert this into your working c program, type \nr from the normal mode inside vim, which will prompt “read snippet /home/ramesh/.vim/c-support/codesnippets/”, type calloc_int_matrix.c at the end and press enter, which will insert the content of the ~/.vim/c-support/codesnippets/ calloc_int_matrix.c to your working file automatically.

    Note: You can define your own code snippets and place it under ~/.vim/c-support/codesnippets/. You can also build your own code snippets from the existing code – select the part of code need to be made as code snippet, press \nw, and give a file-name to it. From next time, type \nr and the file-name to get your custom code snippet.

    There are lot of powerful features in the C-Support Vim Plugin. Read the documentation for more information. The documentation is located in the following location on your system.

    • README : ~/.vim/README.csupport
    • PDF : ~/.vim/c-support/doc/c-hotkeys.pdf
    • Online c-support vim plugin documentation
    • This plugin comes with a help file (csupport.txt) which can be viewed by :h csupport
    •  of this plug-in.

提供使用方法,我做一下简单的总结。
----------华丽分割------------
1.在~/.vim/c-support/templates/Templates可以设置版权信息,如作者、信箱、版权归属等,新建.c或.cpp文件是可以看到,并且列出了使用模板文件位置,可以自己编辑
2.\if    插入一般函数
3.\im    插入主函数
4.\cfu    插入函数头,即函数说明
5.\cfr    插入一个frame comment,可以用来写变量说明
6.\p中
7.\rc    保存并编译
8.\rr   运行
9.\nr    可以在~/.vim/c-support/codesnippets中编写一些预编译代码或者代码块,通过此命令使用
---------------------------------------------
总之c.vim是个不错的c/c++辅助代码插件 , 恩.
---------------------附:详细的c.vim hotkey-------------------------
-- 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* comment /">       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)
  \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)
  \sw       while                               (n,i)
  \swh      while { }                           (n,v,i)
  \ss       switch                              (n,v,i)
  \sc       case                                (n,i)
  \s{       { }                                 (n,v,i)
  -- Preprocessor -------------------------------------------------------
  \p                         (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=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  package
    script version
    date
    Vim version
    user
    release notes
        

    5.5
    2009-02-17
    7.0
    


    + Additional plugin-tags (jump targets in templates): , .
+ Additional mapping Ctrl-j : jump to these new targets.
+ Template-file: additional macro |STYLE| and IF-ENDIF-construct to easily
  choose between sets of templates.
+ Additional mapping: auto-complete classical C comment (also multi-line).
+ Additional mapping: auto-complete open block starting with { .
+ Visual mode for date and time insertion (menu 'Comments').
+ Visual mode for tags (submenu 'Comments->tags (plugin)').
+ Bugfix: hotkey \ica not working
+ Bugfix: hotkey Shift-F2 for the alternate-plugin disappeared. 
                
阅读(891) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~