opened image

Vim text editor

Often there is a need to edit text files directly on a server with a Linux operating system. When installing Linux OS from our templates, the vi/vim text editor is already available in the system, which you can use to edit text files directly on the server.

If for some reason it is not installed, then it can be installed by running the install command. For rpm-based distributions:

yum install vim


For deb-based, the command is:

 

apt-get install vim


In fact, vi and vim commands can run the same application with different settings. In earlier versions of Linux, the vim editor may not be installed, but there is always a vi editor.

To work with the program, you need to log in to the server using the SSH protocol. For example, on Windows, by installing the PuTTY program, and on Linux or Mac, use the built-in ssh program. Depending on the Linux system and the program you are using to connect to the server, you can copy and paste from your operating system's clipboard. For example, you can paste text into PuTTY using the SHIFT+INS combination, and copy it by simply selecting the text. Unfortunately, this does not work on all Linux distributions on the server.

In order to start the editor, log in to the server via SSH using any available client - built into your OS or, for example, PuTTYand run the commands:

vi
vim


For existing ones, you can specify both absolute and relative paths (an example with an absolute path is shown):

vi /path/to/file
vim /path/to/file


The vi/vim editor is more complex and advanced than, for example, nano. Its main feature is that it has two main modes: command mode and insert mode. In command mode, almost every letter or combination performs a command. In insert mode, the text is actually entered. Work in command mode is performed only with the Latin keyboard layout. In order to enter the insert mode, you need to press the Latin letter 'i' or the INSERT button in command mode (if you are already in insert mode, the letter 'i' will simply appear under the cursor). Press ESC to exit insert mode.

Consider the appearance of the main window of the vimeditor.

 
Image 1. Appearance of the vim editor in command mode
 
Image 2. Appearance of the vim editor in insert mode


The program has a "clean", ascetic interface, but its capabilities are very large. The program was developed for convenient and fast control exclusively from the keyboard. Therefore, when opening a file, we see only the editing area and at the bottom a narrow status bar that shows the mode (command, insert or visual selection), additional information about the entered commands that appears during the introduction of combined commands, the line number and character in the line. All elements are customizable.

Text navigation is also done differently than nano and other classic editors.

In command mode, you can navigate through the text using both the arrows, the HOME, END, PG UP, PG DOWN buttons, and the letters on the keyboard (the most important commands will be at the end of this section):
H - left
J - down
K - up
L - right
You can move through the words using the W and B buttons.
This is done in order to keep your eyes on the screen and your hands on the keyboard during editing, which greatly increases the speed of work. Most vim commands are executed by pressing  letters on keyboard in command mode.

In insert mode, you can use the arrows, but this does not work in all systems and terminals.

The vim editor also allows you to perform basic editing actions, such as copying, pasting text, searching, replacing. These buttons must be pressed in command mode. The following commands are written as a cheat sheet:
V - highlight text
Y - copy
D - delete selection
DD (successively press the D button twice in a row) - delete a line
DAW (successively press 3 letters on the keyboard) - delete a word
X or DEL - delete the character under the cursor
SHIFT+P - paste before cursor
O - add a line after the current one and switch to edit mode
SHIFT+O - add a line before the current one and switch to edit mode
U - undo action
CTRL+R - repeat action
/ (slash) - search by word
N - when searching, go to the next found
SHIFT+N - when searching, go to the previous found

As mentioned above, to enter the insert mode, you need to use the I or INSERT button. In this case, the input will begin before the character on which the cursor was located. To enter insert mode and add characters after the cursor, press the A button. Also, to enter insert mode at the beginning of a line, press SHIFT+I, at the end of a line, press SHIFT+A.

Separately, it is worth describing the search and replace process. The vim editor uses regular expressions for this. Getting to know them is a separate topic that goes beyond the scope of this article. In general, if you switch to search mode (button '/' in command mode) and enter a word you want to find and hit ENTER, the program will find it. Next, press the N or SHIFT+N to continue searching.

In order to replace one word with another in the entire file in the vim editor, you need to switch to a special submode of command mode - commands mode - and enter a special command. To switch to commands mode, enter the character ':'(colon) while in command mode. Enter all characters:

:%s/old/new/g

where old is the old word to be replaced; new - new to replace with. At the end, press the ENTER key.

For example, I will replace the word Lorem with Hello:

 
Image 3. Replacing a word in vim
 

After hitting the ENTERbutton:

 
Image 4. Replacement done


This way you can replace, for example, the name of the site in the dump of the mysql database. Example:

:%s/example.com/domain.net/g


Now the most important question regarding the vi/vim editor, namely: how to get out of it. To do this, switch to commands mode (by pressing ':' in command mode) and type one of the following sequences:
:q - exit if no changes were made
:q! - if changes have been made but you do not want to save them
:w - write (save) file
:wq - write file and exit

If there are difficulties working with the vim editor, then in order to leave the editor and start all over again, press the ESC key several times to reset all intermediate modes and to exit the insert mode and type on the keyboard :q! . If necessary, several times.

 

 

Conclusion

The vim editor is well suited for those who want to learn and learn a lot. In experienced hands, it can greatly increase your productivity in writing text and code. Once you've spent some time getting to grips with it, you won't want to use any other editor on a Linux server.