<HTML ><HEAD ><TITLE > Moving around. </TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.57"><LINK REL="HOME" TITLE=" C editing with VIM HOWTO " HREF="index.html"><LINK REL="PREVIOUS" TITLE=" Introduction " HREF="intro.html"><LINK REL="NEXT" TITLE=" Jumping to random positions in C files " HREF="random.html"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >C editing with VIM HOWTO</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="intro.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="random.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="MOVING" >2. Moving around.</A ></H1 ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN29" >2.1. w, e and b keystrokes</A ></H2 ><P > One can use the <B CLASS="KEYCAP" >w</B >, <B CLASS="KEYCAP" >e</B > and <B CLASS="KEYCAP" >b</B > keys to move around a file. VIM is capable of recognizing the different tokens within a C expression. </P ><P > Consider the following C code </P ><DIV CLASS="FIGURE" ><A NAME="AEN36" ></A ><P ><B >Figure 1. A C snippet </B ></P ><DIV CLASS="MEDIAOBJECT" ><P ><IMG SRC="moving1.png" ></IMG ></P ></DIV ><DIV CLASS="TEXTOBJECT" ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" > ... if(( NULL == x ) && y > z ) ... </PRE ></TD ></TR ></TABLE ></DIV ></DIV ><P > Assume that the cursor is positioned at the beginning of the <B CLASS="KEYCAP" >if</B > statement. By pressing w once the cursor jumps to the first <B CLASS="KEYCAP" >(</B >. By typing <B CLASS="KEYCAP" >w</B > again the cursor moves to <B CLASS="KEYCAP" >NULL</B >. Next time the cursor will move to the <B CLASS="KEYCAP" >==</B > token. Further keystrokes will take you as follows. <B CLASS="KEYCAP" >x.</B >.. <B CLASS="KEYCAP" >)</B >... <B CLASS="KEYCAP" >&&</B >... <B CLASS="KEYCAP" >y</B >... <B CLASS="KEYCAP" >></B >... <B CLASS="KEYCAP" >z</B >... and finally <B CLASS="KEYCAP" >)</B >... </P ><P > <B CLASS="KEYCAP" >e</B > is similar to <B CLASS="KEYCAP" >w</B > only that it takes you to the end of the current word and not to the beginning of the next word. </P ><P > <B CLASS="KEYCAP" >b</B > does the exact opposite of <B CLASS="KEYCAP" >w</B >. It moves the cursor in the opposite direction. So you can moving backwards using the <B CLASS="KEYCAP" >b</B > keystroke. </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN63" >2.2. {, }, [[ and ]] keystrokes</A ></H2 ><P > The <B CLASS="KEYCAP" >{</B > and <B CLASS="KEYCAP" >}</B > 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. </P ><P > For Example </P ><DIV CLASS="FIGURE" ><A NAME="AEN69" ></A ><P ><B >Figure 2. Another C snippet </B ></P ><DIV CLASS="MEDIAOBJECT" ><P ><IMG SRC="moving2.png" ></IMG ></P ></DIV ></DIV ><P > The above snippet shows two paragraphs. One can easily move from the beginning of one to the other, by using the <B CLASS="KEYCAP" >{</B > and <B CLASS="KEYCAP" >}</B > keys. <B CLASS="KEYCAP" >{</B > will take the cursor to the paragraph above and <B CLASS="KEYCAP" >}</B > will take the cursor to the paragraph below. </P ><P > Many people have the coding style where a logical set of statements are grouped together and separated by one or more blank lines. </P ><P > For Example </P ><DIV CLASS="FIGURE" ><A NAME="AEN83" ></A ><P ><B >Figure 3. Another C snippet </B ></P ><DIV CLASS="MEDIAOBJECT" ><P ><IMG SRC="moving3.png" ></IMG ></P ></DIV ></DIV ><P > The <B CLASS="KEYCAP" >{</B > and <B CLASS="KEYCAP" >}</B > keys are very useful in such situations. One can very easily move from one "paragraph" to another. </P ><P > Another set of keys which are useful are the <B CLASS="KEYCAP" >[[</B > and <B CLASS="KEYCAP" >]]</B > keys. These keys allow you to jump to the previous { or next { in the first column. </P ><P > For Example </P ><DIV CLASS="FIGURE" ><A NAME="AEN97" ></A ><P ><B >Figure 4. The next snippet of C code </B ></P ><DIV CLASS="MEDIAOBJECT" ><P ><IMG SRC="moving4.png" ></IMG ></P ></DIV ></DIV ><P > Lets say you were editing foo() and now you want to edit bar(). Just type <B CLASS="KEYCAP" >]]</B > 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 <B CLASS="KEYCAP" >[[</B > the cursor will move to the first { above i.e. the beginning of bar() itself. One has to type <B CLASS="KEYCAP" >[[</B > again to move to the beginning of foo(). The number of keystrokes can be minimized by typing <B CLASS="KEYCAP" >2[[</B > to take the cursor to the beginning of the previous function. </P ><P > Another set of similar keystrokes are the <B CLASS="KEYCAP" >][</B > and <B CLASS="KEYCAP" >[]</B > keystrokes. <B CLASS="KEYCAP" >][</B > takes the cursor to next } in the first column. If you were editing foo() and wanted to go to the end of foo() then <B CLASS="KEYCAP" >][</B > will take you there. Similarly if you were editing bar() and wanted to go to the end of foo() then <B CLASS="KEYCAP" >[]</B > would take the cursor there. </P ><P > The way to remember the keystrokes is by breaking them up. The first keystroke will indicated whether to move up or down. <B CLASS="KEYCAP" >[</B > will move up and <B CLASS="KEYCAP" >]</B > 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 }. </P ><P > One caveat of the <B CLASS="KEYCAP" >]]</B >, <B CLASS="KEYCAP" >][</B >, <B CLASS="KEYCAP" >[[</B > and <B CLASS="KEYCAP" >[]</B > keystrokes is that they match the braces which are in the <I CLASS="EMPHASIS" >first column</I >. 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 </P ><P > <B CLASS="COMMAND" > :map [[ ?{<CTRL-VCTRL-M>w99[{ </B > </P ><P > <B CLASS="COMMAND" > :map ][ /}<CTRL-VCTRL-M>b99]} </B > </P ><P > <B CLASS="COMMAND" > :map ]] j0[[%/{<CTRL-VCTRL-M> </B > </P ><P > <B CLASS="COMMAND" > :map [] k$][%?}<CTRL-VCTRL-M> </B > </P ></DIV ><DIV CLASS="SECT2" ><H2 CLASS="SECT2" ><A NAME="AEN132" >2.3. % keystroke</A ></H2 ><P > The <B CLASS="KEYCAP" >%</B > 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. </P ><P > Amongst other things, the <B CLASS="KEYCAP" >%</B > keystroke can be used to match #if, #ifdef, #else #elif and #endif also. </P ><P > This keystroke is very useful in validating code that one has written. For Example </P ><DIV CLASS="FIGURE" ><A NAME="AEN139" ></A ><P ><B >Figure 5. The next snippet of C code </B ></P ><DIV CLASS="MEDIAOBJECT" ><P ><IMG SRC="moving5.png" ></IMG ></P ></DIV ></DIV ><P > Checking the above code will involve checking the correctness of the parenthesis. The <B CLASS="KEYCAP" >%</B > 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. </P ><P > Similarly the <B CLASS="KEYCAP" >%</B > can also be used to jump from a { to its corresponding }. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="intro.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="random.html" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Introduction</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Jumping to random positions in C files</TD ></TR ></TABLE ></DIV ></BODY ></HTML >