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



== PART 3 ==

make

grep

:make

:grep

make

grep

:cn

:cp

:cn5

:cp5

:c5



Macros



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.



Example:

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



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



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,



CTRL + T OR CTRL + O .



You can directly go to a variable or function,



vim –t <VARIABLE NAME>



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



:ts <VARIABLE NAME>



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.



Yet another gr8 feature is VIM macros. You can record a series of actions and use it whenever needed.To Record :- handle is where you wanna record. This will start to record your actions.again will stop recording.will record to a,will record to f.To Execute:- handle is where the recording is :Dwill execute the macro recorded in a,will execute the macro in f.will execute the macro in a 5 times.Example:Suppose you want to comment printf in a couple of c files.- Start VIM(-r is for grep_ing recursively)(this will start recording to c)(now in insert mode)- Type // (commenting printf)(back in normal mode)(save the file)(stop recording)(Now c has the recorded data.)(go to the next place where printf is)(will add the // and save the file)- Repeat :)Ctags is a way to traverse source tree very easily and effectively. You must installbefore using. You can get latest version here... To generate tags, runat the parent directory.- Recursive- All filesThis will generate a file namedin 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,ORIt 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,ORYou 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 ???

After the outstanding response to Part 2, here is the Part 3 of the series on various Tips & Tricks for using VIM Effectively...We can runandfrom inside VIM. How ? :)Justorwith arguments (of course) :DMake :It will executeand if any errors or warnings found, it will directly go the particular locations in the files !!!Grep :It will executeand 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.(see next) - Go to the next entry(see previous) - Go to the previous entry- Skip 4 entries forward- Skip 4 entries backward- Go to entry 5You 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 :)