Monday, June 20, 2011

Vim or VI editor tutorial in UNIX

VI Editor is like notepad in UNIX but it’s extremely powerful and have sophisticated feature to work as complete IDE. No matter which version of UNIX you are working or which flavor you are using you always find either VI editor or VIM there. VI is a very large topic and I am not covering every aspect of it what I am sharing here is some examples of using VI editor in UNIX or Linux. most of the time we use only 10% of VI editor and never know 90% of it , to be frank I also don't know many of VI feature by heart but I always search for it , see people working in VI and learn from there experience as well. after working on VI editor what I found that every day we learn something new in VI editor but we forget something equally important and there I thought lets document whatever VI commands I have learned so far and using regularly. Idea is to keep this VI Editor tutorial updated with every single useful VI commands I learn and I also ask you guys to contribute with something useful. If you see 10 peoples working in VI editor you realize that they use it differently then use some new VI commands which you are not familiar of and which could improve your productivity as well. so if you would like to know more and more about VI editor I would suggest observe people around you when they work in VIM or VI editor and learn from there experience. Don’t confuse with both VIM and VI, VIM stands for VI improved and has some more feature than VI editor. Many a times you will find that VI command is aliased to VIM to make it default editor in UNIX.

Long story short let get into VI command examples:

Opening file in Read only mode in VI Editor

If you are in production server then you definitely want to open a config file in read only mode to avoid any accidental change. So if you are not using less you can use "-R" option of VI editor:

vim -R config.xml

And if you want to save any change from read only mode you can do by ":wq!".

Navigation commands in Vim or VI

One secret of being efficient in VI editor is to be fluent in navigating between different parts of file. Any time you should be able to go to start of file, end of file, start of line, end of line, and search for a particular word from up and down and then move around. Navigational commands are executed from inside VI editor, here are some of must know examples:

gg -- goes to start of fileshift g -- goes to end of file0 -- goes to beginning of the line$ -- goes to end of the linenG -- goes to nth line:n -- another way of going to nth line

Editing in VI Editor in UNIX

What do we do most in VI editor I would say editing and to become efficient and quick this is an area where you need to practice and become fluent.

Searching pattern in VI Editor

Most of you guys familiar with grep command in UNIX and UNIX find command which is used for searching in Unix, here we will see how to search inside VI. If you have open your log file in VI editor and looking for any word you can search for a particular word or ID from either top to bottom or bottom to top, if you are interested in first occurrence then I would suggest to search from top to bottom on the other hand if you are interested on last occurrence then its better to search from bottom to top

/Exception -- will search for word "Exception" from top to bottom and stop when it got first match, to go to next match type "n" and for coming back to previous match press "Shift + N"

?Exception -- will search for word "Exception" from bottom to top and stop when it got first match, to go to next match type "n" and for coming back to previous match press "Shift + N", remember for next match it will go towards top of file.

Running Shell command from inside VI Editor

Some times we are editing something and we wanted to execute some shell command to get some information, normally we need to close the VI editor then execute shell command and then again open the VI editor this is not a fast way of doing it, if we want to save time and work faster we need to directly execute shell command from VI and we can do this by using "!command" from command mode of VI editor. For example doing “ls” from VI editor we can type:

:!ls

if you wants to go directly to shell without quitting from VI editor you can go by executing !sh from VI and then come back to VI editor by just executing command "exit" from shell.

Some Useful VI Editor options

VI editor is full of options but we don't really use most of them here I am listing some of the option which I use most frequently and found quite useful.

:set nu -- This will display line number in front of each line quite useful if you want line by line information. You can turn it off by executing "set nonu". Remember for turning it off put "no" in front of option, like here option is "nu" so for turning it off use "nonu".

:set hlsearch -- This will highlight the matching word when we do search in VI editor, quite useful but if you find it annoying or not able to see sometime due to your color scheme you can turn it off by executing set nohlsearch.

:set wrap -- If your file has contains some long lines and you want them to wrap use this option, if its already on and you just don't want them to wrap use set nowrap.

:colorscheme -- color scheme is used to change color of VIM editor, my favorite color scheme is murphy so if you want to change color scheme of VI editor you can do by executing "colorscheme murphy ".

:syntax on -- syntax can be turn on and off based on your need , if its on it will display color syntax for .xml, .html and .perl files.

:set ignorecase : This VI editor option allows you do case insensitive search because if its set VI will not distinguish between two words which are just differ in case.

:set smartcase : Another VI editor option which allows case-sensitive search if the word you are searching contains an uppercase character.

Opening multiple files in VI Editor

Sometime we just want to open 2 or 3 files in one go and then wanted to navigate on those. we can do this easily in VI editor by just giving file name separated with space while executing Vim or VI command e.g. vim file1 file2 file3 . For navigating between those file we can use option ":n" for going to next file. You can also open any file at any time from VI editor by executing ":e filename" or reload the same file by just executing ":e" (without file name).

Saving and Quitting from VI Editor in Unix

Once we done with our change in VI editor we either wants to save or quite from the file, here are the commands to do so.

:w -- to save file anytime:wq -- most used to save data and quit:q! -- quit without saving:wq! -- to save change in file opened in read-only mode.

Checking history and getting help in VI editor

if you ask me list one command you always want to remember I would say "history" because by using "history" I can get all commands I have executed in past. Same way by typing "!history" we can get all commands we have executed from VI editor. This is immensely powerful and I use it a lot. Another command which is important to remember is ":help" by typing this you can access the help system provided by VI. If you are interested on any particular command you can type ":help command" and it will display help information for that command.

find and replace in vi editor

don't confuse this with find command in UNIX , VI editor provides easy commands to search and replace, perform global search and local search. I would suggest practice it a few times to get hold of it and understand its working. we use ":s" (substitute) command for search and replace in VI editor.

The “:s” command look for a particular pattern and replaces with provide substitution, here are some examples of using search and replace in VI editor.In Vi editor search and replace, "g" stands for global search here which means all occurrence of a particular word in a line changed rather than just first word, which is default behavior if you don't use global search

:%s/Stock/Equity/g This is an example of global search it will replace all occurrence of word "Stock" in file with word "Equity". Its also equivalent to following command ": 0,$ s/Stock/Equity/g" which actually tells that search from fist to last line.

:%s/Stock/Equity/gc This is similar to first command but with the introduction of "c" it will ask for confirmation

:%s/Stock/Equity/gci This is command is global, case insensitive and ask for confirmation. to make it case Sensitive use "I"

Recording and replaying command in VI editor

Some times we need to perform some repetitive task and we need to execute same command again and again, you might want to consider using VI editor's recording functionality. Recoding in vim or VI editor can be done by using q and the executing recorded comment by using q@1

This list of examples of VI editor is not complete and just a collection of VI commands from top of my head. Please let me know how you are using VI editor in your day 2 day life and share some useful tips here for benefit of all. I also found vim wiki as most detailed and useful resource for VI editor and vim tips so go and learn few tips.