Sunday, July 25, 2021

10 example of using Vim or VI editor in UNIX and Linux

Vim or VI editor tutorial in UNIX 
VI Editor is like notepad in UNIX but it’s extremely powerful and has a 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 editors 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 features by heart but I always search for it, see people working in VI and learn from their 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.

The 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 people working in VI editor you realize that they use it differently then use some new VI commands which you are not familiar with and which could improve your productivity as well. So if you would like to know more and more about VI editor I would suggest observing people around you when they work in VIM or VI editors and learn from their experience. 

Don’t confuse with both VIM and VI, VIM stands for VI improved and has some more features than VI editor. Many times you will find that VI command is aliased to VIM to make it default editor in UNIX.

vi editor in unix

The best example of using Vim or VI editors in UNIX

Long story short let's get into VI command examples:



1.Opening a file in Read-only mode in VI Editor

If you are in the 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 the read-only mode you can do by ":wq!".


2. Navigation commands in Vim or VI

One secret of being efficient in VI editor is to be fluent in navigating between different parts of the file. Any time you should be able to go to the start of the file, end of the file, the start of the line, end of the 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 the file
shift g -- goes to the end of file
0 -- goes to the beginning of the line
$ -- goes to the end of the line
nG -- goes to the nth line
:n -- another way of going to the nth line


3. 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.
vi editor commands in unix

yy -- equivalent to cut also called yank
p -- paste below line
shift p -- paste above line
dd -- deletes the current line
5dd -- deletes 5 lines
u -- undo last change
Ctrl + R - Re do last change



4.  The searching pattern in VI Editor

vim editor and vi editor tutorial
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 to 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 the first occurrence then I would suggest searching from top to bottom on the other hand if you are interested on the 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.

5. 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 a 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 a 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 want 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 the shell.


6. 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.


7. Opening multiple files in VI Editor

Sometimes 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 the option ":n"  for going to the 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).


8. Saving and Quitting from VI Editor in Unix

Once we did with our change in VI editor we either want 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.


9. 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.


10. find and replace in vi editor

unix vi editor exampledon't confuse this with find command in UNIX , VI editor provides easy commands to search and replace, perform a 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 looks for a particular pattern and replaces with providing 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 the default behavior if you don't use the global search


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

:%s/Stock/Equity/gc
    This is similar to the 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 asks for confirmation. to make it case Sensitive use "I"


11. Recording and replaying command in VI editor

Sometimes we need to perform some repetitive task and we need to execute the 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 the most detailed and useful resource for VI editor and vim tips so go and learn few tips.


Related post:

10 comments :

Anonymous said...

good collection of VI commands very useful for beginners will these work in Vim editor 7.3 as well ?

Unknown said...

How to search a path in VI editor, for eg. /home/tomcat/webapps/Facebook

Anonymous said...

Thank you so much for putting available for everyone these commands.
These all are very useful for beginners.

Ataul said...

Thanks for the commands Javin. Interestingly I'm the 2700th follower of your blog. Very informative blog.
Thanks again.

Anonymous said...

Nice examples, some of them can also be used as VIM editor based questions for UNIX and Linux Interviews. You can check whether candidate knows how to do find/replace in VIM, knows how to work with VIM e.g. insert, delete, going to a particular line, changing color scheme and so on.

Anonymous said...

It is very useful & better for understanding.
Thank you

javin paul said...

@Anonymous, thanks you like this VI editor examples.

vilkeshpatel said...

Hi javin is there any way to cross check the files being transferred from windows to linux machine are same? We got so many formatting issues on linux machine because of different formats in windows and linux. Is there any editor on linux to cross check the checksum of file?

Unknown said...

@vilkeshpatel, You could use 'sum' command to check the checksum of the file in Linux. However as long as you are dealing with plain text file you should not see any formatting issues, else other files for e.g. like java source file you could always have integrity with having maintaining same encoding format. If you use eclipse right click on the project or file name goto 'Resource' and yous could see text file encoding and use it as 'UTF-8', hope this helps

coder said...

Very Useful commands

Post a Comment