C editing with VIM HOWTO
Heroor
Revision HistoryRevision v1.0Jan 14, 2001Revised by: shSecond Revision. Corrected some typos.Revision v0.1Dec 04, 2000Revised by: shFirst Revision. I would love to have your feedback
This document gives an introduction to editing C and other language files, whose syntax is similar, like C++ and Java in vi/VIM.
----; Table of Contents; 1. Introduction; 2. Moving around.: ; 2.1. w, e and b keystrokes; 2.2. {, }, [[[ and?] keystrokes; 2.3. % keystroke; 3. Jumping to random positions in C files: ; 3.1. ctags; 3.2. marks; 3.3. gd keystroke; 4. Auto-Completing Words; 5. Formating automatically: ; 5.1. Restricting column width; 5.2. Automatically indent code; 5.3. Comments; 6. Multi-file editing; 7. Quickfix; 8. Copyright; 9. References
The purpose of this document is to introduce the novice VIM user to the editing options available in VIM for C files. The document introduces some commands and keystrokes which will help in improving the productivity of programmers using VIM to edit C files.
The scope of the document is to describe how one can edit C files with VIM. However most of what is described is also applicable for vi. Plus what is mentioned here about editing C files is more or less applicable to C++, Java and other similar languages.
One can use the w, e and b keys to move around a file. VIM is capable of recognizing the different tokens within a C expression.
Consider the following C code
Figure 1. A C snippet
... if(( NULL == x ) 88 y b z ) ...
Assume that the cursor is positioned at the beginning of the if statement. By pressing w once the cursor jumps to the first (. By typing w again the cursor moves to NULL. Next time the cursor will move to the == token. Further keystrokes will take you as follows. x... )... 88... y... b... z... and finally )...
e is similar to w only that it takes you to the end of the current word and not to the beginning of the next word.
b does the exact opposite of w. It moves the cursor in the opposite direction. So you can moving backwards using the b keystroke.
The { and } keys are
used to move from paragraph to paragraph. When editing C files these keys have a slightly different meaning. Here a paragraph is taken as a bunch of lines separated by an empty line.
For Example
Figure 2. Another C snippet
The above snippet shows two paragraphs. One can easily move from the beginning of one to the other, by using the { and } keys. { will take the cursor to the paragraph above and } will take the cursor to the paragraph below.
Many people have the coding style where a logical set of statements are grouped together and separated by one or more blank lines.
For Example
Figure 3. Another C snippet
The { and } keys are
very useful in such situations. One can very easily move from one "paragraph" to another.
Another set of keys which are useful are the [[[__ and __?] keys. These keys allow you to jump to the previous { or next { in the first column.
For Example
Figure 4. The next snippet of C code
Lets say you were editing foo() and now you want to edit
bar(). Just type ]] and the cursor will take you to the opening { of the bar() function. The reverse is slightly different. If you were in the middle of bar() and you type [[[[ the cursor will move to the first { above i.e. the beginning of bar() itself. One has to type [[[[ again to move to the beginning of foo(). The number of keystrokes can be minimized by typing 2[[[[ to take the cursor to the beginning of the previous function.
Another set of similar keystrokes are the ][? keystrokes. ] lib/CachedMarkup.php (In template 'browse' < 'body' < 'html'):257: Error: Pure virtual lib/InlineParser.php:336: Warning: Invalid [] syntax ignored: [[__ takes (...repeated 2 times) lib/CachedMarkup.php (In template 'browse' < 'body' < 'html'):257: Error: Pure virtual
Fatal PhpWiki Error