Vi editor starts in command mode in Unix. There are different types of mode available in Vi. If you want to execute commands in vi then you have to be in “Command” mode and if you want to type text instead of command then you have use “Input” mode.
In Input mode, any character you type is considered as text and is added to the file. You cannot run any command until you exit Input mode. To exit input mode, you have to press the “Escape (Esc)” key.
Let’s now go through most of the useful commands.
i – You can insert text after the cursor
a – If you want to append any text after the cursor then you can use ‘a’ command
o – This command can help to open below line
O – To open above line you can use this command.
:r file – If you want to insert any other file content to the currently opened file in vi then this is the best command to use
You have to press “escape (Esc)” key to come out of the input mode.
Change/Replace Commands in Input Mode:
cw – If you want to change one word then you can use this command.
You have to move cursor at the beginning of the word which you want to replace with other word, type cwcommand and then type another new word. Need to press Ecs to come out of the insert mode.
cc – This command is used to change entire line. This command will replace entire line with blank line.
r – You can replace any single character using this command. For e.g. if you want to replace character y with tfrom any word, take your cursor before y and then press rt.
R – Using this command you can replace character(s) or entire word. You just have to keep on typing new word and it will be over written on old one.
s – You can replace/substitute just one character with string
S – Substitute entire line with text. This is similar to cc command which we have discussed above
. – If you want to repeat last change, you can use this command. One of the very useful command available in vi
I hope now you will be able to use vi editor easily.
All above discussed commands are very easy to remember and use. There are some more useful commands available in unix which we will be discussing soon.