Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > by-pkgid > 965e33040dd61030a94f0eb89877aee8 > files > 870

howto-html-en-20080722-2mdv2010.1.noarch.rpm

<HTML
><HEAD
><TITLE
>	 Jumping to random positions in C files
      </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="	 Moving around.
      "
HREF="moving.html"><LINK
REL="NEXT"
TITLE="	 Auto-Completing Words
      "
HREF="auto-complete.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="moving.html"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="auto-complete.html"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="RANDOM"
>3. Jumping to random positions in C files</A
></H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN152"
>3.1. ctags</A
></H2
><P
> 
	    A Tag is a sort of placeholder. Tags are very useful in
	    understanding and editing C. Tags are a set of
	    book-marks to each function in a C file. Tags are very
	    useful in jumping to the definition of a function from
	    where it is called and then jumping back.
	 </P
><P
>	    Take the following example.
	 </P
><DIV
CLASS="FIGURE"
><A
NAME="AEN156"
></A
><P
><B
>Figure 6. 	       Tags Example
	    </B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="tags.png"
></IMG
></P
></DIV
></DIV
><P
>	    Lets say that you are editing the function foo() and you
	    come across the function bar(). Now, to see what bar()
	    does, one makes uses of Tags. One can jump to the
	    definition of bar() and then jump back later. If need be,
	    one can jump to another function called within bar() and back.
	 </P
><P
>	    To use Tags one must first run the program ctags on all
	    the source files. This creates a file called tags. This
	    file contains pointers to all the function definitions and
	    is used by VIM to take you to the function definition. 
	 </P
><P
>	    The actual keystrokes for jumping to and fro are
	    <B
CLASS="KEYCAP"
>CTRL-]</B
> and <B
CLASS="KEYCAP"
>CTRL-T</B
>. By
	    hitting <B
CLASS="KEYCAP"
>CTRL-]</B
> in foo() at the place
	    where bar() is called, takes the cursor to the beginning of
	    bar(). One can jump back from bar() to foo() by just
	    hitting <B
CLASS="KEYCAP"
>CTRL-T</B
>. 
	 </P
><P
>	    ctags are called by
	 </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	    <TT
CLASS="PROMPT"
>$ </TT
><B
CLASS="COMMAND"
>ctags options file(s)</B
>
	 </PRE
></TD
></TR
></TABLE
><P
>	    To make a tags file from all the *.c files in the current
	    directory all one needs to say is 
	 </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	    <TT
CLASS="PROMPT"
>$ </TT
><B
CLASS="COMMAND"
>ctags *.c</B
>
	 </PRE
></TD
></TR
></TABLE
><P
>	    In case of a source tree which contains C files in
	    different sub directories, one can call ctags in the root
	    directory of the source tree with the -R option and a tags
	    file containing Tags to all functions in the source tree
	    will be created. For Example.
	 </P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="SCREEN"
>	    <TT
CLASS="PROMPT"
>$ </TT
><B
CLASS="COMMAND"
>ctags -R *.c</B
>
	 </PRE
></TD
></TR
></TABLE
><P
>	    There are many other options to use with ctags. These
	    options are explained in the man file for ctags.
	 </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN183"
>3.2. marks</A
></H2
><P
> 
	    Marks are place-holders like Tags. However, marks can be
	    set at any point in a file and is not limited to only
	    functions, enums etc.. Plus marks have be set manually by
	    the user.
	 </P
><P
>	    By setting a mark there is no visible indication of the
	    same. A mark is just a position in a file which is
	    remembered by VIM. Consider the following code
	 </P
><DIV
CLASS="FIGURE"
><A
NAME="AEN187"
></A
><P
><B
>Figure 7. 	       The marks example
	    </B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="marks.png"
></IMG
></P
></DIV
></DIV
><P
>	    Suppose you are editing the line x++; and you want to come
	    back to that line after editing some other line. You can
	    set a mark on that line with the keystroke
	    <B
CLASS="KEYCAP"
>m'</B
> and come back to the same line later
	    by hitting <B
CLASS="KEYCAP"
>''</B
>.
	 </P
><P
>	    VIM allows you to set more than one mark. These marks are
	    stored in registers a-z, A-Z and 1-0. To set a mark and
	    store the same in a register say j, all one has to hit is
	    <B
CLASS="KEYCAP"
>mj</B
>. To go back to the mark one has to hit
	    <B
CLASS="KEYCAP"
>'j</B
>.
	 </P
><P
>	    Multiple marks are really useful in going back and fro
	    within a piece of code. Taking the same example, one might
	    want one mark at x++; and another at y=x; and jump between
	    them or to any other place and then jump back.
	 </P
><P
>	    Marks can span across files. To use such marks one has to
	    use upper-case registers i.e. A-Z. Lower-case registers are
	    used only within files and do not span files. That's to
	    say, if you were to set a mark in a file foo.c in register
	    "a" and then move to another file and hit
	    <B
CLASS="KEYCAP"
>'a</B
>, 
	    the cursor will not jump back to the previous
	    location. If you want a mark which will take you to a
	    different file then you will need to use an upper-case
	    register. For example, use <B
CLASS="KEYCAP"
>mA</B
> instead of
	    <B
CLASS="KEYCAP"
>ma</B
>. I'll talk about editing multiple
	    files in a later section. 
	 </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN205"
>3.3. gd keystroke</A
></H2
><P
> 
	    Consider the following piece of code.
	 </P
><DIV
CLASS="FIGURE"
><A
NAME="AEN208"
></A
><P
><B
>Figure 8. 	       The third example
	    </B
></P
><DIV
CLASS="MEDIAOBJECT"
><P
><IMG
SRC="gd.png"
></IMG
></P
></DIV
></DIV
><P
>	    For some reason you've forgotten what y and z are and want
	    to go to their declaration double quick. One way of doing
	    this is by searching backwards for  y or z. VIM offers a
	    simpler and quicker solution. The <B
CLASS="KEYCAP"
>gd</B
>
	    keystroke stands for Goto Declaration. With the cursor on
	    "y" if you hit <B
CLASS="KEYCAP"
>gd</B
> the cursor will take
	    you to the declaration :- struct Y y;. 
	 </P
><P
>	    A similar keystroke is <B
CLASS="KEYCAP"
>gD</B
>. This takes you
	    to the global declaration of the variable under the
	    cursor. So if one want to go to the declaration of x, then
	    all one needs to do is hit <B
CLASS="KEYCAP"
>gD</B
> and the
	    cursor will move to the declaration of x. 
	 </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="moving.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="auto-complete.html"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Moving around.</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Auto-Completing Words</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>