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 has three operating modes:
| 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 |
| 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 |
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/
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:
nyyFor example, the following command will copy 16 lines onto the clipboard:
16yyTo past the selection, just type the letter p.
Cutting-pasting n lines is similar by using the command:
nddThen p to paste!