VI (Visual) Editor

VI (visual) Editor

The VI editor is the most popular and classic text editor in the Linux family. It is available in almost all Linux Distributions. It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs.

Nowadays, there are advanced versions of the VI editor available, and the most popular one is VIM which is VI Improved. It is wise to learn VI because it is feature-rich and offers endless possibilities to edit a file.

How to open VI editor

Open terminal and type vi, and press enter key

Now to insert text, first we need to switch between command mode to insert mode by pressing "i" key from the keyboard, then we can type text inside vi editor as given below:

 

To save and exit from VI editor we need to switch between insert mode to command mode by pressing "Esc" key from the keyboard. and then we can use :wq <filename> to save the file and exit from the VI editor.

VI Editor Operational Modes

Command mode- By default VI Editor opens in this mode, and it only understands commands. In this mode, we can, move the cursor and cut, copy, paste the text. This mode also saves the changes we have made to the file. Commands are case sensitive that means we should use the right letter case.
Insert mode- This mode is for inserting text in the file. We can switch to the Insert mode from the command mode by pressing 'i' on the keyboard. Once we are in Insert mode, any key would be taken as an input for the file on which we are currently working. To return to the command mode we need to press the Esc key and save the changes we have made. 
Last Line Mode (Escape Mode)- Last Line Mode or Escape Mode is invoked/called by typing a colon : , while VI is in Command Mode. The cursor will jump to the last line of the screen and VI will wait for a command. This mode enables us to perform tasks such as saving files, executing commands.

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

Moving within a file

k - Move cursor up

j - Move cursor down

h - Move cursor left

l - Move cursor right

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