全部博文(127)
分类: LINUX
2012-01-11 20:54:31
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:
This article explains how to install the plugin in 3 easy steps and 7 powerful features of the 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 ~/.vimrcAdd 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 fileWhen 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
Now, when you create a new c file, it will show the modified values for AUTHOR and COMPANY as shown below.
Note: To add custom fields to the header, modify the ~/.vim/c-support/templates/file-description.template file and add your own custom field.
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).
Fig1:Insert C Function Automatically
Fig 2:Insert C Function Automatically
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
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.
Fig 4: Insert C Function Header Automatically
Fig 5: Insert C Function Header Automatically
To add a frame comment, type \cfr in normal mode, which will give the following formatted comment.
Fig 6: Insert a Frame Comment Automatically
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 \nrThe 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.noindentFor 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.