Vi and Vim Modes Explained through Examples

This tutorial explains how to navigate between different modes of the Vi and Vim and how to quit or exit from the Vi or Vim editing mode after saving the file.

Vi is the universal text editor of Linux. You can use this editor to edit text files on any flavor, version, and mode of Linux. Vim is the improved version of the Vi editor. As far as functionality is concerned, both Vi and Vim work similarly and use the same commands and modes to control text editing.

In the following section, we will understand Vi (Vim) modes in detail. We will understand Vi (Vim) editing commands in the next part of this article.

This article is the second part of the article "How to use Vi and Vim text editors on Linux". Other parts of this article 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 Text Editors commands Explained

This is the third part of the article. It explains all essential Vi and Vim text editors' commands in detail through examples.

Since the topics discussed in this tutorial applies to both Vi and Vim similarly, I will use Vim to refer to both. In the discussions that follow, whenever I use Vim, assume it for both Vi and Vim.

How does Vim work?

When we edit a file with Vim, 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, Vim applies them to the copied file, stored into the buffer. After editing when we execute the save command, Vim replaces the original file from the modified file.

Vim does not alter the original file until we save the changes. If we quit without saving the file, Vim 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, simply quit from the file without saving it.

Using Vim to create a new file or to edit an existing file

To create a new file or edit an existing file, type vim followed by the name of the file you want to create or edit. If the specified file exists on the specified location, Vim opens that file for editing.

If the specified file does not exist on the specified location, Vim creates a temporary file into a buffer and uses that to store all editing. After editing if the user executes the save command, Vim stores the temporary file to the specified location with the specified name. If the user exists without saving, Vim discards the temporary file.

Let's create a simple text file to understand how it works practically. Access a shell prompt and run the following command.

$vim test-file

Unlike the vi editor, by default, the vim editor is not installed in all Linux flavors. So if you get "command not found" error when running the above command, it means that the vim editor is not installed. In that case, you can either install the Vim editor or can use the Vi editor for practice.

When we execute this command, Vim checks the current directory for the filename test-file. If a file with the same name exists in the current directory, Vim opens that file for editing otherwise it opens a new temporary file with the same name.

If it is a new file, Vim displays a bunch of tildes (~) marking empty lines and a status line at the bottom of the screen.

The status line shows:

  • Name of file including its full path
  • Type of file (whether it is a new file or 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 in the current directory.

vi create a new file

Since it's a new file and contains no characters and lines, characters and lines fields are hidden.

Vi (Vim) Modes

Vim works in two modes: the command mode and the insert mode.

Command mode is the default mode. When we open a file with the Vi editor, we are placed in this mode. This mode is used to control the editing operations such as cut, copy, paste, delete, move, replace, select, remove, navigation, save, and exit. In this mode, we are not allowed to edit or insert a text.

Insert mode is used to edit, update, and append text in the file. It is also used to remove or delete text from the file.

How the keyword will work depends on which mode is selected. If the command mode is selected, the keys will be used to execute the commands. If Insert mode is selected, the keys will be used to edit the text of the file.

To switch between both modes, the Escape key is used.

vim mode

By default, vim opens the file in the command mode and we are not allowed to insert any text in this mode. Let's switch in insert mode and edit the file with some dummy contents.

To switch into the insert mode, first press the 'Esc' key then press the 'i' key.

The following image shows how to switch into the insert mode.

vim command switch to insert mode

Once you are in the insert mode, you can add new text or remove existing text. Let's add a few lines.

vim adding text to file

In insert mode, we can only edit or insert text. We cannot perform any text manipulation or control functions such as cut, copy, save, etc. To switch back to command mode, press the Escape key.

The following image shows this operation.

vim return to command mode

Once you return to command mode, you can perform text manipulation (eg, cut, copy, paste, etc.) and file control operations (eg, save and exit, exit without saving, etc.).

We will learn the Vim commands that are used to manipulate the text in the next part of this article. In this section, let's understand the commands that are used to control the Vim session.

Save and continue editing

If you want to save changes without quitting from the file, use the following command.

:w

The colon (:) is the part of the command.

The following image shows this command with the output.

vim command save and continue editing

Save with a different file name

If you want to save the file with a different name, use the following command.

:w [new-file-name]

In our example, we have used the name "test-file" for our file. Now, suppose we want to save the file with the name "demo-file", then we will use the following command.

:w demo-file

The following image shows how to use this command.

vim save as a new file

Save and exit from the file

To save changes and quit from the file, use the following command.

:wq

The following image shows a sample output of this command

vim save and quit from file

Save the file forcefully

Due to some reasons, if Vim does not save changes, you can instruct it to save changes forcefully. To save changes forcefully, add an exclamation mark (!) at the end of the save command. For example, use the following command to save changes forcefully and quit from the file.

:wq!

The following image shows a sample output of the above command.

vim save file forcefully

Quit from file without saving

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

:q!

The following command shows how to use this command.

vim quit without saving

That's all for this tutorial. In the next part of this article, we will learn Vim commands in detail through examples. If you like this tutorial, please don't forget to share it with friends through your favorite social site.

Advertisements

ComputerNetworkingNotes Linux Tutorials Vi and Vim Modes Explained through Examples