In a production environment, Linux is generally installed in mode console and is command line administered. That is why it is essential to edit files in a command line way, and one of the most popular tool to do this is VIM (VI iMproved).


On most Linux distributions, vim is generally installed by default, but you can install it with the command :

sudo apt-get install vim

Now you can launch the editor by typing the command vim : your computer window turns black, your are in the editor, in the Interactive mode.

VIM operates in 3 modes:

Interactive mode : it is the default mode. In launching VIM, you are in the interactive mode. In this mode, you can not write text. The interactive mode allows you to move in the text, to delete a line, to copy and paste text, to join a particular line, to cancel your actions, etc. Each action can be initiated by a pressing on a touch of the keyboard (for instance, we press on x to delete the character where the cursor is placed).

Insertion mode : in this mode you will be able to enter text and the sheath may be inserted at the cursor’s location. To switch to this mode, there are several ways. One of the most common way consists in pressing the touch i (insertion). To switch back to the interactive mode, you have to press the touch Esc (Echap)

Commande mode : This mode can launch commands such as « leave », « save », etc. You can also use it to activate Vim options (like syntax highlighting, lines number display…). To activate this mode, you need to be in the interactive mode and press the colons key « : ». You will confirm the command with the Enter key and switch back to the interactive mode.

The interactive mode

It is the most sensitive mode for beginners, because each touch of the keyboard matches a function, so it is necessary to become familiar with it.

Here are the most useful :

to switch on insertion mode, to enter text.

to switch on insertion mode, to insert according to the cursor’s position

to move at the start and end of the line

to move from word for word

delete letters

deleted words, lines etc (it would be more appropriate to talk about “cut”, you will understand later)

delete a line : Press twice on d to delete any line where the cursor could be.
You can precede this instruction with a number of lines to delete. For instance, if you enter 2dd, you will delete two lines all at once. Nothing will then appear on the screen as long as you you did not type 2dd in its entirety : VIM stores contact your command information : first the number of line (here, 2), then the command to launch (d for delete) then the command’s execution on the whole line with the second d.

delete a word. The word will be deleted between the cursor and the next space key. If you put the cursor in the middle of a word and execute the command dw, then the last half of the word will disappear.
As with the deletion of the two consecutive lines with 2dd, you will be able to delete, for example, 4 consecutive words with the command 4dw (or d4w for delete 4 words)

you delete from the cursor to the start of the line.

you delete from the cursor to the end of the line.

to copy a line to memory, in the same way as dd “cut” a line (and not delete actually…).

to copy a word

to copy from the cursor to the end of the line etc. This way, characters will be loaded with VIM memory, to be able to…

If you cut text with dd or copied text with yy (or one of their equivalent, dw, y$ etc.) then you can paste it with the p key. The text will be pasted to the line located after the cursor. You can specify the number of times when the text has to be pasted by processing it in the command, for example 7p will paste 7 times the cut or pasted text where the cursor is.

replace a letter: Put the cursor on the letter to replace. Enter « r » followed by the letter you intend to stand in for. For instance, rs replaces the current letter with a « s ».

to cancel modifications (the counterpart of your CTRL+Z ou CMD+Z). You can specify the number of actions to cancel: 7u will cancel the last 7 actions.

Skip to the line n° X: For example, you can directly skip to the line n° 7 by typing 7G (be careful, it is a capital « G », so please think about pressing the Shift key).
To skip to the last line, simply enter G.
To return to the first line, enter gg.

Command mode

To switch in the command mode, you have to be in the interactive mode (Esc if you are in the insertion mode) then press on the colons key « : ». A kind of prompt will appear at the bottom of the screen, waiting for you to enter the command to execute.

here are the most useful:

Allows to save your file. You can specify the file’s name in parameter, for example :w MyFile.conf

 Allows to leave VIM. If you changed your file, VIM will prevent you from leaving. So you can force the exit by adding an exclamation mark :q!

Both combined w and q commands will allow to save and leave VIM, probably the most used by the Linux administrators.

 Searches and replaces text, by passing in parameter the former word and the new word:

:s/former/new : replaces the first occurence on the line where the cursor is
:s/former/new/g : replaces all the occurrences on the line where the cursor is
:#,#s/former/new/g : replaces all the occurrences on the lines n° # to # of the file
:%s/former/new/g : replaces all the occurrences in the whole file.

You can insert a file at the cursor’s position. You have to specify the name of the file to insert, for example :r FiletoInsert (you can use the autocompletion with the Tab key)

You can cut the screen in a horizontal position, where the cursor is. Without parameters, your file will be opened a second time. You can specify a file to open :sp OtherFile (there is no limit, you can separate your screen as often as you want, or at least as often as the monitor size will allow it)

Allows you to cut the screen in a vertical position and operates like :sp

Allows you to launch an external command. For example :!ls will display the folder’s content where you are located without having to leave VIM

The options

You can configure VIM by activating options. You can directly activate them from VIM but they will be “forgotten” as soon as you leave VIM. The most efficient is to change the conf file, leave then restart VIM

vi /etc/vim/vimrc

Here are the most useful options:
This clearly is the first option to activate : syntax highlighting. According to the type file you are opening, VIM will color the text.

Displays the line numbers

 Displays the commands you are executing (remember the command 2dd for example)

 Displays the line numbers -> Ignore the case during characters searches