3. Jumping to random positions in C files

3.1. ctags

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.

Take the following example.

Figure 6. Tags Example

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.

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.

The actual keystrokes for jumping to and fro are CTRL-] and CTRL-T. By hitting CTRL-] 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 CTRL-T.

ctags are called by

           $ ctags options file(s)
         

To make a tags file from all the *.c files in the current directory all one needs to say is

           $ ctags *.c
         

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.

           $ ctags -R *.c
         

There are many other options to use with ctags. These options are explained in the man file for ctags.

3.2. marks

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.

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

Figure 7. The marks example

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 m' and come back to the same line later by hitting ''.

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 mj. To go back to the mark one has to hit 'j.

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.

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 'a, 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 mA instead of ma. I'll talk about editing multiple files in a later section.

3.3. gd keystroke

Consider the following piece of code.

Figure 8. The third example

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 gd keystroke stands for Goto Declaration. With the cursor on "y" if you hit gd the cursor will take you to the declaration :- struct Y y;.

A similar keystroke is gD. 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 gD and the cursor will move to the declaration of x.