Vi and Vim Modes Explained through Examples
Vim (vi) is a universal text editor. It is available on all distributions and versions of Linux. Vi is the first visual text editor. It lacks many modern features. Vim is the updated version of vi. It addresses all shortcomings of vi and provides many additional features to make text editing straightforward.
So far, functionality is a concern; both work similarly and use the same commands and modes to control text editing. Both use two modes to control text editing. These modes are command and insert. The command mode controls text manipulation commands, such as copy, paste, delete, find, replace, etc. The insert mode allows users to add, edit, update, and remove text strings. The Esc key switches between both modes.
How does vim (vi) work?
When we edit a file with vim (vi), it copies that file from its stored location (usually the hard disk) to a block of RAM, known as the buffer. Whatever changes we make, it applies them to the copied file stored in the buffer. After editing, when we execute the save command, it replaces the original file with the modified file.
It does not alter the original file until we save the changes. If we quit without saving the file, it does not replace the original file with the modified file. This feature allows us to edit any text file without worrying about what will happen if we make any mistake. If anything goes wrong, quit the file without saving it.
Installing vim
Vim is part of the default installation. However, for any reason, if it is not available, use the following command to install it.
On Ubuntu/Debian
$sudo apt-get install vim
On Redhat/Fedora/CentOS
#dnf install vim
Using vim (vi) to create a new or edit an existing file
To create a new or edit an existing file, specify the file name as the argument to the vim command. If the specified file exists, it opens that file for editing. If not, it creates a temporary file in RAM and uses it to store all editing. After editing, if we execute the save command, it saves the temporary file at the specified location with the specified name. If we quit without saving, it discards the temporary file.
Let us take an example. Access a shell prompt and run the following command.
$vim test-file
It checks the current directory for the test-file file. If the file exists, it copies it in RAM and opens it for editing. If not, it opens a new temporary file with the given name in RAM. If it is a new file, it displays a bunch of tildes (~), marking empty lines and a status line at the bottom of the screen. The status line shows the following information.
- Name of the file, including its full path
- Type of file (whether it is a new file or an existing file)
- Total number of lines and characters in the file
- The current position of the cursor in the file
- Where you are in the file (i.e. Top, bottom, etc.)
The following image shows the output of the above command when a file with the same name does not exist.

Since it's a new file and contains no characters and lines, characters and lines fields are unavailable.
Vim (vi) modes
Vim works in two modes: the command mode and the insert mode. The command mode is the default mode. It controls the editing operations such as cut, copy, paste, delete, move, replace, select, remove, navigation, save, and exit. It does not allow us to edit or insert text. Insert mode allows us to edit, update, and append text in the file. It also lets us remove text from the file.
How the keyword will work depends on which mode is selected. If the command mode is selected, the keys will execute the commands. If Insert mode is selected, the keys will edit or update the text in the file. The Esc key lets us switch between modes.

By default, vim opens the file in the command mode. The command mode does not allow us to insert any text. Let us switch to insert mode.

Once we are in the insert mode, we can add or remove text. Let us add a few lines.

In insert mode, we can edit or insert text. We cannot perform text manipulation operations, such as cutting, copying, deleting, etc. To perform these actions, we have to switch back to the command mode. Press the Escape key to switch back to command mode.

Once we return to command mode, we can perform text manipulation operations (cut, copy, paste, etc.) and file control operations (save and exit, exit without saving, etc.).
Save and continue
To save changes without quitting from the file, use the following command.
:w
The colon (:) is the part of the command.

Saving with a different file name
To save the file with a different name, use the following command.
:w [new-file-name]
The following command saves the file with the name demo-file.
:w demo-file

Save and exit the file
To save changes and quit from the file, use the following command.
:wq

Saving the file forcefully
Add an exclamation mark (!) at the end of the save command to save changes forcefully. For example, use the following command to save changes forcefully and quit the file.
:wq!

Quit the file without saving
If you have made a mistake or changed something you do not want to store, you can use the following command to exit the file without saving changes.
:q!

This tutorial is part of the tutorial series 'vi Editor in Linux'. Other parts of this series are the following.
Chapter 1 Features of Vi and Vim Explained with Differences
Chapter 2 Vi and Vim Modes Explained Through Examples
Chapter 3 Vi and Vim Text Editors Commands Explained
Conclusion
Vim (vi) works in two modes: command and insert. In command mode, it lets us manipulate text using various commands. In insert mode, we can add, update, and delete text. It uses the Esc key to switch between both modes.
By ComputerNetworkingNotes Updated on 2025-09-30