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 –
It is available in almost all Linux Distributions
It works in the same manner across different platforms and distributions
It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs
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 Nano, and Vile. It is wise to learn vi because it is feature-rich and offers endless possibilities to edit a file.
Uses of vi editor:
Create a file
Edit a file
Modes of vi editor:
Command mode
Insert mode
How to open a file using vi command:
Command mode
Command mode allows you to view the content of the file and move the cursor up and down. Command mode will not allow to type the character or text.
Insert mode
Insert mode will allow you to type the text or update the file.
Explore with below options with vi text editor
Options available in vi text editor (Under Command mode):
- Insert at cursor (goes to insert mode)
a - Write after cursor (goes to insert mode)
A - Write at the end of line (goes to insert mode)
ESC - Terminate insert mode
u - Undo last change
U - Undo all changes to the entire line
o - Open a new line (goes to 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 onwards
s - Substitute one character under cursor and continue to insert
S - Substitute entire line and begin to insert at the beginning of the line
~ - Change case of individual character
Note: You should be in the "command mode" to execute these commands. vi editor is case-sensitive so make sure to type the commands in the right letter-case.
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.
Moving within a file
k - Move cursor up
j - Move cursor down
h - Move cursor left
l - Move cursor right
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.