Linux - The Vi editor

November 2016

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:

:s/chain_to_be_replaced/replacement_chain/
IThe replacement can be made throughout the entire document with the following syntax:
:%s/chain_to_be_replaced/replacement_chain/

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:

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

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

ndd
Then p to paste!

Related :


Linux - El editor de Vi
Linux - El editor de Vi
Linux - Editor Vi
Linux - Editor Vi
Linux - L'éditeur Vi
Linux - L'éditeur Vi
Linux - L'editore Vi
Linux - L'editore Vi
Linux - o editor Vi
Linux - o editor Vi
This document entitled « Linux - The Vi editor » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.