Article overview

Help article

What is Vi in Linux?

Linux has several text editors to open files. The best known are Nano and Vi, but the most important to learn to work with is Vi. The main reason for this is that there are commands in Linux that have similarities with Vi (in Ubuntu, this is usually not the case). If you can handle Vi, it makes the use of Linux as a whole easier. In addition, by default, vi is present on every distro, which isn’t always the case for nano.

In this article, we explain the basic functions of Vi and demonstrate them with a few animations.

If you are used to Windows or MacOS, Vi has a higher entry threshold than some other editors like Nano. The commands that are used often show few similarities with programs which you encounter in Windows or MacOS. Once they have gotten used to it, however, most people prefer to use Vi.


Vi or Vim

Vi was designed in 1982. The old Vi has some properties that are not relevant in modern systems, for example, no support of arrow keys and it has no color system. For that reason, Vi Improved is developed, or Vim.

Instead of Vi, Vim is included on modern Linux distros by default, or Vi is updated to support the modern features of Vim. This article is therefore based on the use of Vim and not Vi. When speaking about Vi in Linux, people (almost always) mean Vim.

Some distros use the older Vi, for example Ubuntu 16. You update Vi by installing Vim over it with:

apt -y install vim

Opening files

In Linux, every file is a text file. You open a file with Vi via the command: 

vi /folder/filename

Suppose you want to view /var/log/messages, here, the command would then be vi /var/log/messages


Command mode & insert mode

After opening the file, you cannot directly start editing text because Vi opens in command mode.

Command mode means that you can enter commands, but not directly edit the text (see the video at the bottom of this section). Insert mode means that you actually edit the text.

To edit text, close and save vi, use the following key combinations / commands:

vi general

In the animation below you can see how these commands work. For this, we use a test file /var/log/test.

vi general


Navigating in files 

With Vi, you can use the arrow keys to move the cursor through a file. You often work with larger files and in that case, that is not the easiest way of working. Vi has a number of useful methods to quickly go through a file to where you want to go.

  • The commands below only work from command mode.
  • If you are searching forward with /, then, when searching backward with N, the / will change to ? and vice-versa.

vi navigation

In the video below you can see how these commands work. For this, we use an existing log file in CentOS /var/log/messages (/var/log/syslog in Ubuntu, a combination of /var/log/auth.log and /var/log/daemon.log in Debian).

vi navigation


Cut copy paste

You cannot use a mouse in Vi. Fortunately, Vi has tools to make cutting, copying and pasting relatively easy (and faster than in Nano).  

The commands below only work from command mode.

vi cut copy paste

You can see how these commands work in the video below.

vi cut copy paste


 

You now have an excellent understanding of Vi to be able to edit texts with Vi. Should you have any questions left regarding this article, do not hesitate to contact our support department. You can reach them via the ‘ContactUs’ button at the bottom of this page.

If you want to discuss this article with other users, please leave a message under 'Comments'.

Has this article been helpful?

Create an account or log in to leave a rating.

Comments

Create an account or log in to be able to leave a comment.