14 VI Editor - Linux
14 VI Editor - Linux
Tutorial
What is the VI editor?
The VI editor is the most popular and classic text editor in the Linux family.
Below, are some reasons which make it a widely used editor –
Nowadays, there are advanced versions of the vi editor available, and the
most popular one is VIM which is Vi Improved. Some of the other ones are
Elvis, Nvi, Nano, and Vile. It is wise to learn vi because it is feature-rich and
offers endless possibilities to edit a file.
vi Command mode
vi Editor Insert mode
How to use vi editor
vi Editing commands
Moving within a file
Saving and Closing the file
vi Command mode:
vi <filename_NEW> or <filename_EXISTING>
And if you specify an existing file, then the editor would open it for you to
edit. Else, you can create a new file.
VI Editing commands
i - Insert at cursor (goes into insert mode)
a - Write after cursor (goes into insert mode)
A - Write at the end of line (goes into insert mode)
ESC - Terminate insert mode
u - Undo last change
U - Undo all changes to the entire line
o - Open a new line (goes into insert mode)
dd - Delete line
3dd - Delete 3 lines.
D - Delete contents of line after the cursor
C - Delete contents of a line after the cursor and insert new text.
Press ESC key to end insertion.
dw - Delete word
4dw - Delete 4 words
cw - Change word
x - Delete character at the cursor
r - Replace character
R - Overwrite characters from cursor onward
s - Substitute one character under cursor continue to insert
S - Substitute entire line and begin to insert at the beginning of the
line
~ - Change case of individual character
Make sure you press the right command otherwise you will end up making
undesirable changes to the file. You can also enter the insert mode by
pressing a, A, o, as required.
You need to be in the command mode to move within a file. The default
keys for navigation are mentioned below else; You can also use the arrow
keys on the keyboard.
Saving and Closing the file
Shift+zz - Save the file and quit
:w - Save the file but keep it open
:q - Quit without saving
:wq - Save the file and quit
You should be in the command mode to exit the editor and save
changes to the file.
Summary:
The vi editor is the most popular and commonly used Unix text editor
It is usually available in all Linux Distributions.
It works in two modes, Command and Insert
Command mode takes the user commands, and the Insert mode is
for editing text
You should know the commands to work on your file easily
Learning to use this editor can benefit you in creating scripts and
editing files.