VI is a screen-oriented text editor originally designed for UNIX and UNIX-based operating systems. VI 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 flavors of Linux Distributions,
- It works the same across different platforms and Distributions,
- It is user-friendly.
Hence, millions of Linux users love it and use it for their editing needs.
In recent times there be an advanced version 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.
To work on the VI editor, you need to understand its operation modes. They can be divided into two main parts.
- Command mode: In commands mode, VI only understands commands, you can only move the cursor and cut, copy, and paste the text. In the command, you can modify and change the file. Remember that Commands are always case Sensitive. Insert mode is always used to insert text into the file.
- Insert Mode: To switch to the Insert mode from the command mode by pressing 'i' on the keyboard. Once you are in Insert mode, any key would be taken as input for the file on which you are currently working.
To return to the command mode and save the changes you have made you need to press the Esc key.
VI-Editor Usage
1. To launch the VI Text-Editor, Open the Terminal and type vi <file name>.
It will open a new interface:
As you see VI editor opens in command mode.
Always remember that if you specify an existing file, then the editor would open it for you to edit, else, you can create a new file.
2. Press “I” to enter the insert mode.
Here I type Hello World.
3. Press ‘ESC’ to Enter Command mode. Type :wq to save and Quit.
To check the content of the file we use cat the file name here the file name is first_file.
VI editing Commands
There are several commands available in the VI text editor.
Note: The "command mode" to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter case.Here are all the commands used in the VI Text editor.
Keystrokes |
Action |
I |
Insert at cursor (goes into insert mode) |
A |
Write after the cursor (goes into insert mode) |
A |
Write at the end of the 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
|
D |
Delete the contents of the line after the cursor |
C |
Delete the contents of a line after the cursor and insert new text. Press the ESC key to end insertion. |
dw |
Delete word |
cw |
Change word |
x |
Delete the character at the cursor |
r |
Replace character |
R |
Overwrite characters from cursor onward |
s |
Substitute one character under the cursor and continue to insert |
S |
Substitute the entire line and begin to insert at the beginning of the line |
~ |
Change case of individual character |
Moving within a file
You need to be in 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.
Keystroke |
Use |
k |
Move cursor up |
j |
Move cursor down |
h |
Move cursor left |
l |
Move cursor right |
Saving and Closing the file
You should be in the command mode to exit the editor and save changes to the file.
Keystroke |
Use |
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 |
In a short time, I hope, I have completely described the whole thing, if there are any doubts then write them down in my comment session.