Home » Applications » How to Use Vim Text Editor

How to Use Vim Text Editor

Experienced Linux users often use the terminal because it allows them to perform necessary actions much faster. While working with a computer, we quite often have to edit various files. The Linux operating system has several text editors that work in the terminal. Most often, beginners use the nano editor, but if you've noticed, on this site, all articles use the vi text editor. I find Nano inconvenient and not functional enough.

There is a much better text editor, and that is the vi editor. It supports quick navigation through text, convenient editing, commands for changing its settings, executing terminal commands from within the editor, as well as plugins to extend its functionality. However, it is a bit complex for beginners and very unconventional.

In this article, we will explore how to use vim, delve into the basics of working with this editor, as well as its main commands.


Table of Contents

Editing a file in Vim

If you need to quickly edit a file and you don't want to read the entire article, in this section I've provided a brief guide on how to make changes and exit the editor. To open a file, execute:

vim /path/to/the/file

Please note that for the editor commands to work properly in command mode, you need to select the English keyboard layout. By default, editing commands will not work in the Cyrillic layout.

To enter insert mode (editing), press i. Here you can edit the file just like in nano. Once you're done, press Esc to switch to command mode, type :wq, and press Enter. This will save and exit. That's it, you're done.

If you want to exit without saving changes, use :q! instead of :wq.

Editor Versions

Currently, there are three versions of the editor - vi, vim, and neovim. Vim stands for Vi Improved, an enhanced version of vi. This new version has brought many improvements. It is the version used in most modern distributions. Therefore, if I write vi, it means I assume the use of vim.

Additionally, there is NeoVim, which is an improved version of Vim with support for asynchronicity, more features, and plugins. However, I have not yet switched to this new version, so I can't say much about it.

Vim Modes

The Vim text editor can operate in several modes. This is its main feature. The first mode, which is used by default when opening the editor, is the command mode. In this mode, you can use letter keys to control the editor, search, and also use the command line. All of this should be done in the English layout. At least by default.

The second mode is the insert mode. This is regular text editing, it works the same way as text editing in nano. To switch to insert mode, use the i key. To switch back to command mode, use the Esc key.

There is also a visual mode for text selection, a replace mode that is activated when you use character replacement commands, and a command-line mode where you can enter various vi commands, which is activated by typing a colon ":" in command mode.

How to Use the Vim Editor

We will start, as usual, with launching the program, as well as the options that can be passed to it. The syntax of Vim is very simple:

$ vim options filename

Or:

$ vi options file_name

If the vim package is installed on your system, the vi command will refer to the vim executable. In this case, there is no difference between these commands. Simply running vim without specifying a file name will open an empty window for editing a new file. Now, let's look at the main command options:

  • +number - move the cursor to the specified line after launch.
  • +/pattern - perform a search by pattern and move the cursor to the first occurrence.
  • "+command" - execute a command after the program starts.
  • -b - binary mode, for editing executable files.
  • -d - diff mode, for finding differences in files, multiple files need to be specified for opening.
  • -g - graphical mode.
  • -n - do not use autosave to recover the file in case of a crash.
  • -R - read-only mode.
  • -w - save all actions to a file.
  • -x - encrypt the file when writing.
  • -C - Vi compatibility mode.

1. Moving the cursor

In command mode, you can navigate through the editable text and perform actions on it using letter keys. This mode opens by default when the editor starts. Most commands consist of a single character, usually a letter or a symbol. However, many commands accept parameters. For example, in cursor movement commands, you can type a number before the command to specify how many characters or lines to move. For beginners, it can be very confusing at first that in command mode, characters are interpreted as commands.

The following commands are used to move the cursor:

  • h - move one character to the left;
  • l - move one character to the right;
  • j - move one line down;
  • k - move one line up;
  • w - move one word to the right;
  • b - move one word to the left;
  • H - move to the bottom of the screen;
  • G - move to the end of the file;
  • gg - move to the beginning of the file;
  • <number>G - move to a specific line;
  • . - move to the last edit.

You can launch the editor and experiment to make it easier to understand how it works. Start with h, l, j, k - many users use them to navigate through text not only in Vim, because it allows you to keep your hands on the keyboard. If you press a number before pressing the letter key, the command will be repeated several times. For example, 3j will move the cursor up three lines.

Very often, there is a need to move to the beginning or the end of a line. Vim commands can help with this. Use the ^ command to move to the beginning of a line or the $ command to move to the end of a line. Note that to type these commands, you need to hold down Shift and press 4 or 6.

2. Insert Mode

To switch to insert mode, the following commands are used:

  • i - insert text at the cursor position, the character under the cursor will be replaced;
  • I - insert text at the beginning of the line;
  • a - append text starting from the cursor position;
  • o - insert a new line below the current one and start editing;
  • O - insert a new line above the current one and start editing;

These commands also accept repetition symbols. Experiment with them; you might achieve interesting and somewhat unexpected results. When you finish editing, you can switch back to command mode by pressing the Esc key.

3. Vim Command Line

The Vim command line is activated in command mode by pressing the colon - ":". Here, commands are available for saving changes, configuring the interface, exiting the editor, and interacting with the external shell. Below are the most commonly used commands in the Vim editor:

  • :w - save the file;
  • :q - close the editor;
  • :q! - close the editor without saving;
  • :e file - read the contents of the file into the cursor position;
  • :r file - insert the contents of the file into the next line;
  • :r! - execute a shell command and insert the output into the editor;
  • :set variable=value - set the value of a variable, for example, set tabstop=4, this command can be used to control many aspects of vim's operation.
  • :buffers - view open files;
  • :reg - view the contents of the clipboard.

4. Exiting Vim

Thus, to exit Vim, you need to switch to command mode by pressing Esc, activate the command line with a colon, and execute the q command:

And to exit without saving changes, type :q! and press Enter. We've covered the basics, and I hope using vim doesn't seem so daunting to you now. But this is far from all; this powerful editor can do much more. Next, we'll look at a few examples of using vim to make it easier for you to get to grips with the new program.

5. Deleting Characters and Lines

You can edit text not only in the normal mode but also in the command mode using commands. Sometimes, it's even more convenient than in the editing mode. For example, you can delete characters. For this purpose, the following commands are used:

  • d - delete a character;
  • dd - delete the entire line;
  • D - delete characters from the cursor to the end of the line.

These vim editor commands work a bit differently. After pressing one of them, nothing will happen. We can also specify how many times to repeat the command, but we also need to indicate the number of characters it will apply to and the direction using the cursor movement keys. This is done after pressing the command. For example, to delete one character to the right, type dl. To delete two characters to the right of the cursor, press d3l, and to delete three lines down - d3j.

6. Character Replacement

In command mode, you can not only delete characters but also replace them with others. There are two commands for this:

  • r - replace the current character;
  • R - replace multiple characters.

The first command allows you to replace one character and return to command mode. The second command will replace all characters until you return to command mode by pressing the Esc key:

7. Undo and Redo Actions

In regular text editors, you can use Ctrl+Z to undo the last actions. In the Vim editor, there is its own history of actions. It is available in command mode. If you want to undo the last change, simply press u. Note that each subsequent press of this button will undo the previous change made in command mode. For example, if you deleted one character with the d command, it will be restored.

However, if you switch to insert mode and make a lot of changes there, and then return to command mode, all these changes will be considered as one. If you accidentally undo something important, you can redo the action by pressing Ctrl+r:

Here are some commands for undoing and redoing actions:

  • u - undo the last action;
  • U - undo the last action in the current line;
  • . - repeat the last action;
  • Ctrl+r - redo the last undone action.

8. Variables in Vim

I want to delve deeper into variables. As mentioned earlier, in Vim, you can set variable values using the set command, and this will affect the editor's functionality. Variables can contain a string value or a flag. For example, the following command can be used to enable line number display:

:set number

The command sets the value of the number variable to true. To disable the display of line numbers, you need to set it to false. This can be achieved by inverting the flag's value:

:set number!

Additionally, you can reset the variable to its factory settings using &:

set number&

9. Selection and Clipboard

The Vim text editor supports text selection without using a mouse. Therefore, you can copy and paste text even in TTY environments, without a graphical interface. Press v when you are in command mode to switch to selection mode. Then, you can use the cursor movement keys h, j, k, l to select the desired characters or lines:

Now you can perform any actions with the selected text. For example, you can delete it by pressing the d key. To clear the selection, press the Esc key. That's not all. Vim has its own clipboard that can be used for copying and pasting text. This clipboard consists of several registers. Unless specified otherwise, all commands work with the default clipboard. Here are the basic commands for copying and pasting text:

  • y - copy to clipboard;
  • yy or Y - copy line to clipboard;
  • x - cut to clipboard;
  • p - paste from clipboard.

The commands for copying and cutting work similarly to the commands for deleting text. You can specify the number of characters and the direction, or simply select the text. Please note that each running instance of Vim has its own separate clipboard register, and it is not linked to the system clipboard. Therefore, you will not be able to copy text from one program to another without additional actions or settings.

If you want to copy data to the system clipboard, first you need to make sure that Vim installed on your system supports this. To do this, execute the command:

vim --version | grep clip

If this feature is supported, you will see +clipboard or +xterm_clipboard. Let's get back to the registers. Vim has several clipboard registers, and one of them is linked to the system clipboard. This is the "+" register. You can display all registers and their contents using the command:

:reg

The register named "" is the default clipboard. If you want to copy text to the system clipboard, you need to type the register name before the copy command. For example, press "+ and then y. Note that you need to use the Shift key to get the desired character. You need to press Shift+' and Shift+=.

As you can see, this is quite complex, especially if you're not accustomed to using these keys. There's a simpler way. You can copy the text to the default register and then copy it from there to the system clipboard using the Vim command line. To do this, copy the text as usual, and then use the following command:

:let @+=@"

After this, you will be able to insert text anywhere in the operating system. Directly linking the system clipboard with Vim's clipboard is a bad idea because Vim places all deleted characters into the clipboard, and thus, if you delete something, it will overwrite what you had in the clipboard before.

10. Search and Replace in Vim

Quite often, we need to find a specific sequence in text. The Vim text editor is capable of doing this. Firstly, if you need to find a character in a line, press f and type the desired character, the cursor will move to its position.

To search through the entire file, use the / command. After it, you need to enter the word you want to find. To search for the next occurrence, use n, for the previous one - N.

Here are the main commands for searching:

  • f - find a single character;
  • / - search for multiple characters;
  • n - find the next occurrence;
  • N - find the previous occurrence.

A slightly different command will be used for the replacement:

:%s/string_to_search/string_to_paste/g

For example, let's replace all occurrences of the word Alice with Robert:

:%s/Alice/Robert/g

A colon starts a command shell with the s command for substitution. The % symbol means that the entire file should be processed, and g indicates that all found lines should be processed, not just the first one. To make the program ask before each substitution, you can add the c option at the end of the line.

11. Editing Multiple Files

To open multiple files, simply pass them as parameters when launching the program:

vim /etc/default/grub /etc/default/cron

The vim editor in Linux will open the first file. To switch to the second file, use the command :n, and to go back, use :N. With the command :buffers, you can view all open files, and with the command :buffer 1, you can switch to the first file.

Built-in Tutorial

I would also recommend you to take the built-in tutorial in the editor. Completing all the tutorial tasks will take 25-30 minutes. This will help you consolidate all the knowledge gained from this article in practice. The fact is that vim has a lot of commands and key combinations, and it's impossible to remember all of them without practice. To start the tutorial, execute the following command:

vimtutor

But it's not necessary to do this now, this article contains all the essential basic information, and after reading it, you will already be able to confidently use vim, and you can go through the training a bit later.

Wrapping Up

In this article, we have explored how to use vim text editor. This is far from all its capabilities, but now you can confidently handle the editor and forget about nano. As you can see, the editor contains a lot of functions, and it might be difficult to remember all of them at once. But once you get the hang of it, you might want to enable Vim keybindings support in other programs as well, because it's convenient.

Leave a Comment