Select font size:          

 

Basic use of the vi/vim editor

The vi is the only editor that is by default installed on any Linux distribution. Any system administrator should have basic knowledge of its usage. The vim is an enhanced version of the vi. In this small tutorial we use vi and vim equally.

As the use of the vi is not self-explaining, we recommend to copy the file that you want to change before modifying. We usually do so as follows:

cp filename filename.orig


How to use the vi/vim

The vi has two modes, the command mode and the insertion mode. When you open a file it is in the command mode. If you want to insert or modify something, navigate to the place in the text and enter i. To get back to the command mode just press "ESC". Instead of i" for "insert" you can also enter a for "append after cursor".


Here's a list of the most important commands:

vi <filename>Create or open a file named <filename>
:wqQuit the file, save changes.
:qExit as long as there are no changes.
:q!Exit file, do not save changes.
:wSave changes.
iInsert before cursor.
aAppend after cursor.
xDelete character after the cursor.
XDelete character right before the cursor.
DDelete to the end of the line.
ddDelete current line.
yYank (copy) - not used alone.
yyYank (copy) the current line, including the newline character.
3yyYank (copy) three lines, starting from the line where the cursor is positioned.
"+yYank (copy) into system clipboard. This command only works in the yet enhanced version of the vim that includes GTK3-GUI, see below.
:u (or :undo)Undo the last change. To do so, press ESC to switch to command mode and enter u. Press u a second time to undo the undo. Enter :undo 3 to undo the last three changes.
:r (or :read)Read from a file and insert into the file in use. Imagine you have created a file called "bashrc-temp" which you want to integrate in the file ".bashrc". Enter the file .bashrc and enter :read bashrc-temp. It will enter the content of the file "bashrc-temp" at the current position in the file ".bashrc".

How to put the content of a file in the clipboard

The vi or the vim cannot copy into the system-wide clipboard, at least not in their normal version. To still be able to copy into the system-wide clipboard, you have to install the vim with GTK3 GUI. The package is normally called "vim-gtk3" or "gvim".

With the command vim --version | grep 'clipboard' you can check if the clipboard is already active. In that case the enhance vim with GTK3 GUI is already present in your system. If you get -clipboard as a result, you have to install the the vim with GTK GUI.

Under Debian or Ubuntu the relevant command is:

sudo apt install vim-gtk3

Now you will be able to copy into the clipboard typing "+y.

Conclusion

With these commands you can do everything you need to modify for instance Linux configuration files. However, if you have to work often with the vi, it can be quite cumbersome to work only with these commands. Obviously, there are many more useful commands that vi powerusers should know. Have a look at one of the many "cheat sheets" (see for instance Other web links).


Check for more Linux related articles.