Saturday, February 26, 2011

VI Tips

After lots of request from my friends, I finally end up with writing something on VI or VIM ( Vi IMproved ). This is one of the most powerful editor in UNIX environment.

Try this
# vi test.txt
[Press ESC]
:help

Please read through that briefly. There are various .txt files just like each chapters in text book.

You can access those .txt file using

For example
:help usr_01

I want you guys to read and learn by yourself. That really helps. You will know what you are doing.

However, I will mention some frequently used shortcuts/commands:

h j k l move left move up move down move right

:x [Enter] To save and exit file
:q To quit
:qw Same as :x
:q! Ignore the changes and quit

:set number Display line number on each line
:set nonumber Don't display line number on each line

:set ruler Display the cursor position at the bottom of the screen

CTRL + g Display the cursor position with percentage of the page

CTRL + d Scroll half window down
CTRL + u Scroll half window up

/findthis Search 'findthis' string
[ Press n or N for searching backward and forward in the file]

The characters .*[]^%/\?~$ have special meanings. If you want to use them in a search you must put a \ in front of them.

:set ignorecase To ignorecase while searching the string/pattern
:set noignorecase

G To go to the bottom of the page
gg To go to the top of the page

yy To copy/yank the line
dd To delete the line
p To paste the yanked/copied line

$ To go to the end of the line
0 To go to the front of the line

{ To go to the end of the paragraph
{ To go to the front of the paragraph

:syntax on To enable syntax highlight
:syntax off


:line_number To go to particular line e.g To go to line 22 , do the following :22 [enter]


SEARCHING FOR A WORD IN THE TEXT

Suppose you see the word "TheLongFunctionName" in the text and you want to find the next occurrence of it. You could type "/TheLongFunctionName", but that's a lot of typing. And when you make a mistake Vim won't find it. There is an easier way: Position the cursor on the word and use the "*" command. Vim will grab the word under the cursor and use it as the search
string. The "#" command does the same in the other direction. You can prepend a
count: "3*" searches for the third occurrence of the word under the cursor.

HIGHLIGHTING MATCHES

While editing a program you see a variable called "nr". You want to check where it's used. You could move the cursor to "nr" and use the "*" command and press "n" to go along all the matches.

There is another way. Type this command:

:set hlsearch

If you now search for "nr", Vim will highlight all matches. That is a verygood way to see where the variable is used, without the need to type commands.

To switch this off:
:set nohlsearch


REPLACE THE WORD

:%s/old_word/new_word/g

This will substitute old_word with new_word globally.

TUNING SEARCHES

There are a few options that change how searching works. These are the essential ones:

:set incsearch

This makes Vim display the match for the string while you are still typing it. Use this to check if the right match will be found. Then press to really jump to that location. Or type more to change the search string.

:set nowrapscan

This stops the search at the end of the file. Or, when you are searching backwards, at the start of the file. The 'wrapscan' option is on by default, thus searching wraps around the end of the file.


Bonus Tips:
If you like one of the options mentioned before, and set it each time you use Vim, you can put the command in your Vim startup file.
Edit the file, as mentioned at |not-compatible|. Or use this command to find out where it is:

:scriptnames

Edit the file, for example with:

:edit ~/.vimrc

Then add a line with the command to set the option, just like you typed it in
Vim. Example:

Go:set hlsearch