Effective Use of VIM - Part 3

Sunday, August 3, 2008

Effective Use of VIM - Part 3

The Effective Use of VIM Series...

» Effective Use of VIM - Part 1

» Effective Use of VIM - Part 2

» » Effective Use of VIM - Part 3

» Effective Use of VIM - Part 4

» Effective Use of VIM - Part 5

After the outstanding response to Part 2, here is the Part 3 of the series on various Tips & Tricks for using VIM Effectively...

== PART 3 ==

Make, Grep

We can run make and grep from inside VIM. How ? :)

Just :make or :grep with arguments (of course) :D

Make :

It will execute make and if any errors or warnings found, it will directly go the particular locations in the files !!!

Grep :

It will execute grep and show each occurance in the files directly. Don’t forget to use -n option in grep (it tells grep to show line numbers).

Once you are at the location of the first output entry, there are commands, very easy to remember.

:cn (see next) - Go to the next entry
:cp (see previous) - Go to the previous entry
:cn5 - Skip 4 entries forward
:cp5 - Skip 4 entries backward
:c5 - Go to entry 5

You can just browse through all the files and do modifications as you go :) Save the modifications, else it will not jump to the next file :)


Yet another gr8 feature is VIM macros. You can record a series of actions and use it whenever needed.

To Record :

q<handle> - handle is where you wanna record. This will start to record your actions. q again will stop recording.

qa will record to a, qf will record to f.

To Execute:

@<handle> - handle is where the recording is :D

@a will execute the macro recorded in a, @f will execute the macro in f.
5@a will execute the macro in a 5 times.

Suppose you want to comment printf in a couple of c files.
- Start VIM
- :grep -rn "printf" * (-r is for grep_ing recursively)
- qc (this will start recording to c)
- i (now in insert mode)
- Type // (commenting printf)
- ESC (back in normal mode)
- :w (save the file)
- q (stop recording)
(Now c has the recorded data.)
- :cn (go to the next place where printf is)
- @c (will add the // and save the file)
- Repeat :)


Ctags is a way to traverse source tree very easily and effectively. You must install ctags before using. You can get latest version here...

To generate tags, run ctags –R * at the parent directory.

-R - Recursive
* - All files

This will generate a file named tags in the parent directory.

Always start vim from the directory where the tags file is located to get the ctags functionality.

You can place the cursor under a variable or function and to jump to the definition,

g] OR CTRL + ]

It will go to the location (file and line number) where it is defined. If there are multiple definitions, it will list all occurrences and you can select one giving its number.

To go back,


You can directly go to a variable or function,


If you are inside VIM, and you need to go to definition of a variable which is not under cursor,


The location to which ctags take you is based on the tags file. So, as you change the code, you must update the tags file.

To be continued...

Like it? Subscribe via RSS

You may have found out unique ways of doing things in VIM, pls share...

See also... » Help and Support Thread

» Linux Commands I Hardly Knew - Reloaded

» Linux Commands I Hardly Knew

» Unlock a PC on Windiows XP ???

ATOzTOA : Latest Headlines


Anonymous said...

There's a subtle difference between Ctrl + T and Ctrl + O.

Ctrl + O goes back to the last known cursor position, this works in any case where the cursor is moved by means other than "hjkl". Ctrl + T, on the other hand, goes back to the cursor position from where the Ctrl + ] was performed.

If your cursor is parked where Ctrl + ] took you, these are effectively the same - but in other instances you should not confuse the two or you may not get the result you want.

Quick Vim addendum: Ctrl + I is the opposite of Ctrl + O and will take you to the next cursor position.

Anonymous said...

CTRL+N also auto completes but text-wise within the file. It's useful for comments (where CTAGS is not available) or when you don't have CTAGS indexes built.

Post a Comment