What is vi editor
Vi or Visual Editor is the default text editor that ships with most Linux systems. This is a terminal-based text editor that users should learn, especially when there are no more convenient text editors in the system. Some other reasons to use Vi include:
- Vi is available on almost all operating systems.
- Reasonable selection of shortcut keys consisting of short keystrokes.
- You can use Vi as a great HTML editor.
- Vi commands are so rich that you don’t need to take your hands off the keyboard.
- The Vi editor creates small files, making it easy to store.
- It’s free.
In this tutorial, we will learn how to start and use the Vi editor and how to work with text files.
Launch Vi Editor
Vi Editor for Linux is a terminal-based text editor, so first you need to open a terminal window. By opening a Linux terminal, you can:
- Create file
- Work on an existing file
Creating a file using Vi
To create a file in Vi, use the following command:
vi file name
Example: vi textfile.txt
To create a file in a specific location, use the following command:
vi / path / to / filename
Opening an existing file
To open an existing file, use the following command:
vi / path / to / filename
Note. A file name with the specified name will be created if it does not already exist in the system.
Before you start working with files, it is important to find out that Vi Editor works in two modes: command mode and insert mode. In command mode, you can mainly navigate through the text, search for words in a file, save a file, etc. You can execute various commands, but you can’t insert anything into your text; for this you need to be in insert mode. Remember that in command mode, your typed keys will act only as commands; however, in insert mode, you can type and edit text.
Switch between modes
When you first create or open a file in Vi, you are in command mode. Although it seems that you can print here, you cannot. To write something, you need to go into insert mode by pressing I key. To return to command mode, you just need to press Esc key.
Note. It is important to note that the Vi editor is case sensitive. Therefore, if your keys do not give the desired results, make sure that you do not accidentally press the Caps Lock key.
Vi usually starts in command mode. You can perform administrative operations with your files, such as saving a file, executing commands on a file, moving the cursor over a file, cutting / pasting and pasting lines and words into a file. Command mode also allows you to find and replace text in a file.
Moving the cursor over a file:
You can usually move the cursor over the text in your files using the up, down, left, and right arrow keys on the keyboard. If you are using a remote terminal and the arrow keys are not working properly, you can use the following keys as a replacement:
Move left: h
Move right: l
Move up: k
Move down: j
To enter text into your file, you must be in insert mode. Everything that you enter in this mode will be considered as text and added to your file.
Working with vi files
After creating a new file or opening an existing file, you can write text in it, first switching to insert mode by pressing I key. You will notice that the vi command itself usually does not appear on the screen. You can enter several lines and click
You can select text to copy, cut and paste only in command mode. To select text, place the cursor to the left or right of the text you want to select, and press v key. Then you can move the cursor to select the text.
Once you have selected the text as described above, you can click Y copy text and X cut the text. To insert text, place the cursor in the desired location and press P key to paste your copied or cut text. Remember that you must be in command mode to complete these operations.
To remove a character from a file, move the cursor until it is on the wrong letter, and then type X key. To delete more characters, say three, enter 3x,
Saving and exiting a file
In command mode, enter : WQ and press Enter to write the file to disk and exit vi. Command : w will only write the file to disk and : d will exit vi without saving the file. You can also split this command, for example, type : w and press Enter to write the file to disk without exiting or type : d exit vi without saving the file.
When you make changes to your file and exit without saving the changes, vi will give you a warning. You can ignore the warning with the command : D! And press enter.
Common vi commands
To work with files in the Vi editor, you will use the following Vi control commands:
|I||Use this command to insert text before the current cursor position.|
|I||Use this command to insert text at the beginning of a line.|
|Use this command to insert text after the current cursor position.|
|about||Use this command to create a new line for text below the current cursor position.|
|X||Use this command to delete a character under the current location.|
|X||Use this command to remove the character in front of the current location.|
|sv||Use this command to remove from the current location to the next word|
|D||Use this command to delete from the current location to the end of the line.|
|dd||Use this command to delete the entire row.|
|Copy and paste|
|yoo||Use this command to copy the current line.|
|P||Use this command to paste the copied text after the cursor.|
|P||Use this command to paste elongated (cut) text before the cursor.|
|cc||Use this command to delete the contents of a string.|
|s||Use this command to replace a character with the character you are writing.|
|R||Use this command to replace the character under the cursor and return to command mode.|
So this tutorial was about the Vi editor. I hope you learned how to create a new file or open an existing one. You also learned to insert, delete, or delete words from a text file.
Working with the Vi editor on Linux