2. Moving around.

2.1. w, e and b keystrokes

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 ) && y > 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... )... &&... y... >... 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.

2.2. {, }, [[ and ]] keystrokes

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 ][ and [] keystrokes. ][ takes the cursor to next } in the first column. If you were editing foo() and wanted to go to the end of foo() then ][ will take you there. Similarly if you were editing bar() and wanted to go to the end of foo() then [] would take the cursor there.

The way to remember the keystrokes is by breaking them up. The first keystroke will indicated whether to move up or down. [ will move up and ] will move down. The next keystroke indicates the type of brace to match. If it same same as the previous keystroke then the cursor will move to {. If the keystroke is different then the cursor will move to }.

One caveat of the ]], ][, [[ and [] keystrokes is that they match the braces which are in the first column. If one wants to match all braces upwards and downwards regardless of whether its in the first column or not is not possible. The VIM documentation has a workaround. One has to map the keystrokes to find the braces. Without spending too much time on mapping, the suggested mappings are

:map [[ ?{<CTRL-VCTRL-M>w99[{

:map ][ /}<CTRL-VCTRL-M>b99]}

:map ]] j0[[%/{<CTRL-VCTRL-M>

:map [] k$][%?}<CTRL-VCTRL-M>

2.3. % keystroke

The % is used to match the item under the cursor. The item under the cursor can be a parenthesis, a curly bracket or a square bracket. By pressing the % key the cursor will jump to the corresponding match.

Amongst other things, the % keystroke can be used to match #if, #ifdef, #else #elif and #endif also.

This keystroke is very useful in validating code that one has written. For Example

Figure 5. The next snippet of C code

Checking the above code will involve checking the correctness of the parenthesis. The % can be used to jump from one ( to its corresponding ) and vice versa. Thus, one can find which opening parenthesis corresponds to which closing parenthesis and use the information to validate the code.

Similarly the % can also be used to jump from a { to its corresponding }.