What is the vi editor?
There are many ways to edit files on a Linux operating system. You can either use the GUI or work on the Terminal. For those who like to go with the Terminal, 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 -
- It is available in almost all 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
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.
In order to work on VI editor, you need to understand its operation modes. They can be divided into two main parts
This mode is for inserting text in the file
You can 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 an 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
Starting the vi editor
To launch the VI Editor -Open the Terminal (CLI) and type
vi <filename_NEW> or <filename_EXISTING>
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
Note: You should be in the "command mode" to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter-case.
|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 |
Delete 3 lines.
|D||Delete contents of line after the cursor|
|C||Delete contents of line after the cursor and insert new text. Press esc key to end insertion.|
| dw |
| Delete word |
Delete 4 words
|x||Delete character at cursor|
|R||Overwrite characters from cursor onward|
|s||Substitute one character under cursor continue to insert|
|S||Substitute entire line and begin to insert at 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.
Moving within a file
You need to be in the command mode in order to move within a file. The default keys for navigation are mentioned below else, you can also use the arrow keys on the keyboard.
|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
Save the file and quit
Save the file but keep it open
Quit without saving
Save the file and quit
- The vi editor is the most popular and commonly used Linux 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 in order to work on your file easily
- Learning to use this editor can benefit you in creating scripts and editing files