Vi and Vim Editor Explained with Examples

This tutorial explains vi commands modes and differences between the vi command and the vim command. Learn the three modes of vi (vim) command in detail.

Why should you learn the vi editor?

The vi is one of the most popular and widely used text editors. By default, it is not only available in all versions and flavors of Linux but also available in each mode of Linux. In several situations such as; emergence mode and remote session, this is only the editor which Linux provides for manipulating the text files.

The only negative side of this editor is that it uses text based commands to perform all essential functions of text manipulation such as; insert, append, cut, copy, paste, find, replace, delete, undo, redo, save and exit.

The reason why Linux administrators like this editor at first place is that this is the only editor which is available in all situations. No matter which Linux flavor (such as RHEL, Ubuntu, Kali, etc.) is installed or in which mode the Linux is currently running, it can be used for creating, editing and updating text files straight from the command prompt.

In short, if you are a person who calls someone else to repair the system, you may ignore this editor. But if you are the person who is called to repair the system, you must learn this editor.

Difference between vi and vim editor

Vim (vi-improved) is the enhanced and improved version of vi editor. It adds the following features and functions to the existing functionality of vi.

  • It includes more features for the programing languages such as; syntax highlighting, code folding, text formatting etc.
  • It includes inbuilt utility for comparing files.
  • It includes undo/redo facility.
  • It supports external scripting language.
  • It can edit compressed files.
  • It can edit remote files over the network protocol.
  • It supports plugins for additional functionality.
  • It supports screen splitting for editing multiple files simultaneously.

vi editor v/s vim editor


As far as functionality is concerned, both editors work in the same manner. Which editor you choose is a matter of personal choice. If you are learning from scratch, you should learn the vim editor instead of the vi editor. Due to added features, learning and using vim editor is much easier than the vi editor. Since vim is based on the vi, when you will learn how to use the vim editor, you will automatically learn how to use the vi editor.

Vi (Vim) Modes

The Vi (Vim) works in two modes; command mode and insert mode.

Command mode is the default mode. When we open a file with 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. We are not allowed to edit or insert text in this mode.

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

How keyword will function is depend on which mode is selected. If command mode is selected then keys will be used to execute the commands. If insert mode is selected then keys will be used to edit the text in file.

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

vi mode

Setup for practice


Although you can use any existing regular text file for practice, but you should always create and use a new text file in a separate directory. Using a new file in separate directory allows you to complete the practice without making any mess in the existing file system.

Access shell prompt and run the following commands.

#mkdir test
#cd test
#vim regular-file-vim

First command creates a new directory named test. Second command changes current directory to this directory. Third command opens the file named regular-file-vim for editing. If file does not exist, it creates a new file with the supplied name and opens it for editing.

Following figure illustrates above commands.

lab setup for practice

Unlike the vi editor, by default, the vim editor is not installed in all Linux flavors. If you get errors when running the vim command, it simply means that the vim editor is not installed. In this case, either install it or use the vi editor for practice.

By default, vim opens file in command mode and we are not allowed to insert any text in this mode.

vi default mode

To switch in insert mode, the Escape + i keys combination is used. Let’s switch in insert mode and edit the file with some dummy contents.

insert mode vi mode

In insert mode, we can only edit or insert the text. We cannot perform any control operations.

To switch back in command mode, Escape key is used. Let’s switch back in command mode. In command mode, use the : + w +q keys combination to save the file and exit from it.

save and exit from file

Let’s repeat this exercise once again, but this time use the vi command instead of the vim command.

Open the same file again with vi command.

open regular file from vi again

Switch in insert mode and edit the file.

vi editing file

Now save the file and exit form it.

saving file

As long as you edit regular text files, you will not see any difference between the vi and vim. To see the difference between vi and vim edit any script or programming file.

Let’s copy any script file from the /etc directory in to the current directory.

copy script file

To see the difference, open this file with both editors one by one.

difference between vi and vim

As we can see in above output, the vim formats the file in more user friendly manner.

That’s all for this part. In next and last part of this tutorial, we will learn how to perform some advanced text formatting operations with the vi and vim editors. Next part of this tutorial is available here.

How to use the vi and vim Editors in Linux

If you like this tutorial, please don’t forget to share it with friends through your favorite social network.


ComputerNetworkingNotes RHCE 7 Study Guide Vi and Vim Editor Explained with Examples