Monday, March 7, 2011

The power of Vim

Command line is one of the main thing most of the people hate about the Linux. But the power of linux comes form command line. Yes its a bit hard to learn and remember all the commands and keywords. But it worth learning. In my humble opinion its is the fastest editor than any GUI apps. Code travesing is so easy. Here I m presenting some of the vim command I frequently use. Read and move fast on your coding and debugging....

Travesing the code. ============= This is the first thing an editor should provide, to traverse through the code. To go to any reference, use

1) g + ] . This will list all the instance of the definition of a variable of a function if there are more than one. Press the number to go to the definition you prefer. To return back, 2) CTRL + t

This works with ctags. Before using the short cut keys, you should create the database using the command(Ctags -R). This command will create tags file.

To search any string in the current file, prese esc and type / Another way is using (SHIFT + *). Move to variable or function name and press (SHIFT + *). It will move to the next instance in the file. Then you can use " n" to move to next instance and "SHIFT + n" to search in backward direction. "SHIFT + g" will move to the end of the file. ":number" will move to the corresponding line number. If you want to find the end of any loops, put the cursor on the bracket and press "SHIFT + %"

"CTRL + e" to move down "CTRL + y" to move up. EDITING For selecting the lines, I use virtual mode. Press "v" then use the arrow keys to select as many line as you want. Then press "y". Use "p" to paste. To delete any blank space before the lines, use coloumn virtual mode by pressing "CTRL + v"

For auto completion, type till whatever you remember and use "CTRL + p" For substitution, %s/arun/avin/gc If you remove gc it will substitute all the instances of arun with avin without asking. If I want to copy from another file, use the following format, e filename Then use virtual mode and copy then type "e + #" . This will bring back to our old file. Then use "p" for pasting. Copying the format: If you want to format a code like intentation etc. Make few lines of code mannualy and then select the reset of the code along with the modified one in visual mode. Then press "=". The rest of the code will also get the intentation.

Undo and Redo
==============
press "u" for undo and
press "ctrl + r" for redo.


To use Marks
==============
http://vim.wikia.com/wiki/Using_marks


How do I repeat an edit on multiple lines in vim?

How do I repeate an edit on multiple lines ?
=================================

lets say for the text
abc1234
def5678
xyz3456

if you hit CTRL  + V with your cursor over the 1, use arrow keys twice to go down two columns, then Shift + i + ,  Then press ESC, your text would look like this:

abc,1234
def,5678
xyz,3456

.Vimrc
==============
Below are the thing to find in my .vimrc



Multiple copies(Vim copy paste buffers)
============================

"ay12 – create a buffer named ‘a’ with 12 lines in it
"ayw – create a buffer named ‘a’ with 1 word in it
"ap – put the contents of buffer named ‘a’

Please note that the named buffer can be any single letter between a – z

Usage: lets say you are writing a new driver and you want to type some words frequently. You can save them to the buffer.

For eg these two words are pmic_tw123 and PMIC_TW123

keep the cursor at the "pmic_tw123" and type "ayw. This creats a buffer named a and copies pmic_tw123 to it

keep the cursor at the "PMIC_TW123" and type "byw. This creates a buffer named b and copies PMIC_TW123 to it.

To paste use "ap and "bp.

We are actually using registers feature of the Vim. you can see the yanked words by issues :registers 

Similarly you can create for lines too.

No comments: