What Is VI
What Is VI
The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words,
it is available in all Linux distros. It is user-friendly and works same on different distros and
platforms. It is a very powerful application. An improved version of vi editor is vim.
The vi editor has two modes:
o Command Mode: In command mode, actions are taken on the file. The vi editor starts
in command mode. Here, the typed words will act as commands in vi editor. To pass
a command, you need to be in command mode.
o Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will
take you to the command mode from insert mode.
By default, the vi editor starts in command mode. To enter text, you have to be in insert mode,
just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on the
screen but you'll be in insert mode. Now you can type anything.
To exit from insert mode press Esc key, you'll be directed to command mode.
If you are not sure which mode you are in, press Esc key twice and you'll be in command
mode.
Using vi
• The vi editor tool is an interactive tool as it displays changes made in the file on the
screen while you edit the file.
• In vi editor you can insert, edit or remove a word as cursor moves throughout the file.
• Commands are specified for each function like to delete it's x or dd.
• The vi editor is case-sensitive. For example, p allows you to paste after the current line
while P allows you to paste before the current line.
vi syntax:
1. vi <fileName>
In the terminal when you'll type vi command with a file name, the terminal will get clear and
content of the file will be displayed. If there is no such file, then a new file will be created and
once completed file will be saved with the mentioned file name.
Example:
1. vi /home/sssit/Downloads/file.txt
Command mode
This is what you'll see when you'll press enter after the above command. If you'll start typing,
nothing will appear as you are in command mode. By default vi opens in command mode.
To start typing, you have to move to the insert mode. At the end of the terminal window,
directory name and file name are displayed.
Insert mode
To move to the insert mode press i. After pressing i we have entered into insert mode. Now
we can write anything. To move to the next line press enter.
Once you have done with your typing, press esc key to return to the command mode.
To save and quit
You can save and quit vi editor from command mode. Before writing save or quit command
you have to press colon (:). Colon allows you to give instructions to vi.
exit vi table:
Commands
Action
:wq Save and quit
:w Save
:q Quit
To exit from vi, first ensure that you are in command mode. Now, type :wq and press enter.
It will save and quit vi.
Vi Commands
Linux vi editor is different from other editors. You have to use different keys to use different
functions. Although, it's quite easy and interesting to use vi editor.
The vi editor commands are case sensitive.
Have a look at the vi commands in the following table.
To switch from command to insert mode:
Command Action
Commands Action
J To move down
K To move up
H To move left
L To move right
To jump lines:
Commands Action
Commands Action
Commands Action
Commands Action
dd Delete a line
Commands Action
Commands Action
Commands Action
Commands Action
/\<he\> Search for the word he (and not for there, here, etc.)
Commands Action
:1,$ s/readable/changed/ Replace forward with backward from first line to the last line
:3,6 s/letters/neww/g Replace forward with backward from third line to the ninth line
Text buffers:
Commands Action