Vi editor is short for the visual editor app and is a command-line based tool used as the default editor in almost all Linux distributions. It is similar to Notepad on Windows or TextEdit on macOS.
In this tutorial, we shall show you how to work with the Vi editor and the most important shortcuts to use that will help you effectively edit any file. Vi has three main modes that you can use including the Command mode, Escape mode, and the Insert mode that we will be discussing today.
1. Command mode
The command mode in the vi editor is the first mode you will get when you open up the vi editor. In this mode, every typed character is considered as a command. Hence, the vi editor will not display or show any of those commands in the window. Moreover, as long as you are in this mode, you can copy, paste, move through the file, or even delete some characters from the file. Additionally, if you are using any of the other two modes and need to return to the command mode, all you need to do is just press the [ESC] key from the keyboard.
To open a file in the vi editor, launch the Terminal and enter the following command:
Where the filename may be a new file that will be created or an already existing file.
If you created a new file that is not already exiting, then you should get something like the below screenshot:
You can use the following shortcuts:
- dd is used to delete a line from the file
- dw is used to delete a word from a file
- x is used to remove a character from a file
- G is used to go to the end of the file
- Cursors are used to move and navigate within the file
2. Escape mode
To be able to use this mode, you need to first be in the command mode, then press the colon “:” character. Typically, during this mode, you can write any command next to the colon character. The forward slash can be used instead of the colon character, and that is in case you need to search the text in the file.
The following commands can be used during the escape mode:
- :wq, is used to save and quit the file. Note that the command is case insensitive, and it appears at the bottom of the screen, as you can see in the below screenshot.
- :q! is used to quit the file without saving.
- :q is used to quit the file.
- :w is used to save the file without quitting it.
- /string is used to search for the string in the file.
You can hit enter then the cursor will highlight the string you are searching for if it is found like the below screenshot:
3. Insert mode
In the insert mode, each keyboard keystroke will be dealt with as a text, and the vi editor will preview it on the screen as entered from the keyboard by the user. This is the mode where you will be allowed to start entering and inputting text into the file.
During the insert mode, you can use the following shortcuts:
- [ESC] + i, is used to insert text
- [ESC] + a, is used to append text
- [ESC] + A is used to append text at the end of a line
- [ESC] + r, is used to replace a character in the file
That’s all about the Vi editor and its usage in Linux. Hopefully, this tutorial helped you to get you ready to edit your files using the vi editor and effectively use its shortcuts.