This tutorial explains how to use Vi and Vim text editors in Linux. Learn all essential Vi and Vim text editors' commands in detail through examples.
Vi is the universal text editor of Linux. If you know how to use the Vi text editor, you can edit any text file on any mode and version of Linux. Vim is the improved version of Linux. Unlike Vi, Vim is not universal. Depending on your Linux flavor, it may be or may not be installed by default.
To know whether the Vim is installed or not, access a shell prompt and execute the "vim" command without any argument and option.
$vim
If Vim is installed, this command will return an introductory page of the Vim. If Vim is not installed, this command will return the "command not found" error.
If Vim is not installed, you can install it just like any other Linux package. For example, the following command installs Vim on Ubuntu.
$sudo apt install vim
Once Vim is installed, instead of Vi, you can use it.
How to use Vi or Vim editor
To use the Vi editor to create a new text file or edit an existing text file, use the following command.
$vi file-name
To use the Vim editor, use the following command.
$vim, file-name
If the specified file exists, Vim opens that file otherwise it creates a new file with the specified name and opens it for the editing.
Vi and Vim modes
Vi and Vim work in two modes: command mode and insert mode.
In command mode, Vim processes keyboard inputs as commands while in insert mode it processes them as their original values. Insert mode is used to insert, add, delete, update, or edit text. The command mode is used to control text editing and text manipulation related operations. To switch between the two modes, the escape key is used.
To learn what advantages Vim has over Vi, features of Vi and Vim, and Vim modes, you can check the previous parts of this tutorial. Previous parts of this tutorial are the following.
Differences between Vi and Vim explained
This is the first part of the article. It explains the differences between Vi and Vim editors in detail.
Vi and Vim Modes Explained through Examples
This is the second part of this article. It explains how to navigate between different modes of the Vi and Vim.
No matter which editor you use, commands explained in the following section apply to both simultaneously. Because of this, to improve readability, I will use the word vim to refer to both.
In the following section, wherever I refer to the word 'Vim', read it for both editors: Vi and Vim.
Creating a test file for practice
Although you can use any existing regular text file for practice, but it would be better if you create and use a new text file in a separate directory. Using a new file in a separate directory allows you to complete the practice without making any mess in the existing file system.
Access a shell prompt and run the following commands to create a new test file named 'regular-file-vim' in the 'test' directory.
#mkdir test #cd test #vim regular-file-vim
The following image shows the above commands with the output.
By default, vim opens the file in the command mode.
Inserting and appending text
Vim uses the cursor position as the base position for editing. Depending on where you want to insert or edit the text in the line, you can use one of the following commands.
- To insert text at the cursor position, press the 'Esc' key and then press the 'i' key.
- To add text after the cursor position, press the 'a' key followed by the 'Esc' key.
- To insert text at the beginning of the line, use the 'I' key along with the 'Esc' key.
- To insert text at the end of the line, use 'A' with the 'Esc' key.
The following image shows how to use the above commands.
Inserting a new blank line
- To insert a new blank line above the line, press the 'Esc' key then press the 'O' key.
- To insert a new blank line below the line, press 'o' key followed by the 'Esc' key.
By default, Vim only inserts one line. To insert multiple lines, we have to specify the number of lines before pressing the O/o key. For example, to insert 3 blank lines above the cursor line, we have to use the 'Escape + 3 + o' keys combination.
The following image shows how to insert blank lines when editing a file via Vim.
Performing cut and paste
To cut a line, use the 'dd' command. This command cuts the line in which cursor remains and puts that line into a memory buffer. To paste the copied line from the buffer, use the 'p' command. This command pastes the copied line in the current line of cursor.
By default, both commands perform their action only one time. It means, the 'dd' command cuts a single line and the 'p' command pastes the copied line one time only.
To cut the multiple lines or to paste the contents from memory buffer multiple times, we have to specify the desired numbers before these commands.
For example to cut three lines, use the 'Escape + 3 + d + d' keys combination. To paste the contents four times, use the 'Escape + 4 + p' keys combination.
The following image illustrates the 'cut and paste' operation practically.
Performing copy and paste
The copy and paste operation also works similar to the cut and paste operation.
To copy a line, use the 'yy' command. This command copies the current line into a memory buffer. To copy multiply lines, specify the number of lines before the 'yy' command. For example to copy 4 lines, use the 'Escape + 4 + y + y' keys combination.
Just like a line, you can also copy a single character or multiple characters and words. To copy a single character from the cursor, use the 'yl' command. To copy a single word from the cursor, use the 'yw' command.
To paste the copied contents, use the same 'p' command.
The following image illustrates a few examples of the 'copy and paste' operation.
Finding text
To find a text string in the forward direction, use the 'Escape + / + [String]' command. To find a text string in the backward direction, use the 'Escape + ? + [String]' command.
To navigate between search results, the n key is used with the Escape key.
To move in the backward direction, use the 'Escape + N' key. To move in the forward direction, use the 'Escape + n'.
The following image illustrates the search operation practically.
Finding and replacing text
To replace the first occurrence of the text, use the following command.
Escape + : + % + s + / + [old text] + / [new text]
To replace all occurrences of the text, use the following command.
Escape + : + % + s + / + [old text] + / [new text] + / +g
The following image shows examples of both commands.
Deleting text
To remove a single character from the cursor position in the forward direction, press the 'x' key followed by the 'Esc' key. To remove a single character from the cursor position in the backward direction, press the 'Esc' key and then press the 'X' key.
The following image shows the delete operation practically.
- To remove a single word after the cursor, use the 'Escape + d + w' command.
- To remove a single word before the cursor, use the 'Escape + d + W' command.
- To delete a line, use the 'Escape + d + d' command.
- To delete multiple lines, specify the number of lines before the 'dd' command.
The following image shows examples of the above commands.
Reverting (undo/redo) the last command or action
To undo the last action or command, press the 'Esc' key and then press the 'u' key. To undo all actions performed in the current line, press the 'U' key after pressing the 'Esc' key. To redo the last command, use the 'Escape + Ctrl + r' command.
The following image shows how to perform the undo/redo operation practically.
Setting line numbers
By default, Vim does not display line numbers. But if required, we can display them by using the 'Escape+:+set +nu' command.
The following image shows how to use this command to display line numbers.
Save and exit
To save changes and quit, use the 'Escape +: + w + q' command. To exit without saving, use the 'Escape +: + q +!' command. If the file is not changed, you can also use the 'Escape +: + q' command to exit.
The following image shows how to use these commands practically.
That's all for this tutorial. If you like this tutorial, please don't forget to share it with friends through your favorite social channel.