Linux - The Vi editor

Introduction to Vi

Despite its very limited ergonomics, Vi i is one of the most popular text editors texte under Unix type systems (with Emacs and pico). Under Linux, there is a free version of Vi called Vim (Vi Improved). Vi (pronounced vee-eye) is an editor that is fully in text mode, which means that all actions are carried out with the help of text commands. This editor, although it may appear of little practical use at first, is very powerful and can be very helpful in case the graphical interface malfunctions.

The syntax to launch Vi is as follows:

vi name_of_the_file

Once the file is open, you can move around by using cursors or the keys h, j, k and l (in case the keyboard does not have any arrow cursors).

Vi modes

Vi has three operating modes:

  • Regular mode: This is the mode you enter whenever you open a file. This mode allows typing commands
  • Insertion mode: This mode makes it possible to insert characters you capture inside of the document. To switch to insertion mode, just press the key Insert on your keyboard or, by default, the key i
  • Replacement mode: This mode allows you to replace existing text by the text you capture. Just hit r again to go to replacement mode and hit the key Esc to return to regular mode

Basic commands

Command Description
:q Quit the editor (without saving)
:q! Forces the editor to quit without saving (even if changes were made to the document)
:wq Saves the document and quits the editor
:filename Saves the document under the specified name

Editing commands

Command Description
x Deletes the character that is currently under cursor
dd Deletes the line that is currently under cursor
dxd Deletes x lines starting with the one currently under the cursor
nx Deletes n characters starting with the one currently under the cursor
x>> Indents x lines to the right starting with the one currently under the cursor
x<< Indents x lines to the left starting with the one currently under the cursor

Searching and replacing

To search for a word in a document, in regular mode, just type / followed by the chain of characters to be searched for and confirm by hitting the Enter key. Use the n key to go from occurrence to occurrence.

To replace a chain of characters by another on a line, you will find a very powerful command in Vi by using the regular expressions. Its syntax is as follows:

IThe replacement can be made throughout the entire document with the following syntax:

Copy-paste and cut-paste

In Vi, it is possible to copy-paste a selection of lines. To do so, just type in the following command to copy n lines:

For example, the following command will copy 16 lines onto the clipboard:
To past the selection, just type the letter p.

Cutting-pasting n lines is similar by using the command:

Then p to paste!
Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM reaches more than 50 million unique visitors per month and is available in 11 languages.


This document, titled « Linux - The Vi editor », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (