Skip to content

Vim

Vim - The Ubiquitous Text Editor.

Introduction

Vim is a text editor developed from vi. Its features such as code completion, compilation, and error navigation are particularly rich and convenient for programming, making it widely used among programmers.

Installation

Linux systems usually come with Vim pre-installed. Open a terminal and type vim to start it.

If manual installation is required, Vim's official website provides documentation for downloading. Compile and install according to your needs.

Vim Modes and Common Keybindings

The basic operations of Vim are explained in the built-in tutorial. Open a terminal and type vimtutor to enter the tutorial.

These operations typically take about twenty to thirty minutes to become familiar with.

Command Mode

The default mode when entering Vim.

In this state, keyboard actions are recognized by Vim as commands rather than input characters. For example, pressing i at this point will not input a character; i is treated as a command.

Vim's arrow keys are ↑, ↓, ←, →, or alternatively h, j, k, l.

1
2
3
4
5
        ↑(k)
        ^
(h)← <     > →(l)
        v
        ↓(j)

Here are commonly used commands in Command Mode:

  • i Switch to Insert Mode, start inputting text at the current cursor position. Press Esc to return to Normal Mode.
  • x Delete the character after the cursor.
  • : Switch to Command-line Mode, to enter commands at the bottom line.
  • a Switch to Insert Mode, start inputting text after the cursor.
  • o Switch to Insert Mode, insert a new line below the cursor; O Switch to Insert Mode, insert a new line above the cursor.
  • p Paste clipboard content below the cursor; P Paste clipboard content above the cursor.
  • dd Delete the entire line where the cursor is located.
  • d The d command is also for deletion, often used in combination with other keys.
  • u Undo the last change to the text.
  • y The y command can copy the selected area. Requires pressing v to enter Visual Mode.
  • yy Copy the current line.
  • Ctrl + r Redo the last undone operation.
  • :w Save the file, often used with q to save and exit.
  • :q Quit Vim.
  • :q! Force quit Vim without saving changes.

Some other commands:

  • The c command is used for change, equivalent to di.
  • The = command can apply automatic indentation in default format to selected lines.
  • == Auto-indent the current line.
  • The . command can repeat the last executed command.
  • The gg command can jump to the beginning of the code; the G command can jump to the beginning of the last line of code; prefixing G with a number jumps to the specified line.
  • w can jump to the beginning of the next word; e can jump to the end of the current or next word; b can jump to the beginning of the current or previous word; 0 can jump to the beginning of the line; $ can jump to the end of the line. w, e, 0, $ can also be combined with other commands, such as de, dw, d0, and d$, which correspond to deleting to the end of the word, deleting to the beginning of the next word, deleting to the beginning of the line, and deleting to the end of the line, respectively.

In Command Mode, press / to bring up the search box at the bottom. Enter the characters to search for, press Enter to view the search results. If there are multiple results, press n to jump to the next result; press N to jump to the previous one.

In Command Mode, press * to search for the word under the cursor.

Before entering a command, typing a number n will cause the command to repeat n times.

Insert Mode

Press i in Command Mode to enter Insert Mode. Press Esc to return to Command Mode.

In Insert Mode, you can use the following keys:

  • Character keys and Shift combinations to input characters.
  • ENTER, the Enter key, for line breaks.
  • BACK SPACE, the Backspace key, to delete the character before the cursor.
  • DEL, the Delete key, to delete the character after the cursor.
  • Arrow keys to move the cursor in the text.
  • HOME/END to move the cursor to the beginning/end of the line.
  • Page Up/Page Down to page up/down.
  • Insert to toggle the cursor between insert and replace mode; the cursor will become a vertical line/underscore.
  • ESC to exit Insert Mode and switch to Command Mode.

In Insert Mode, press Ctrl+o to enter "Insert - Command Mode". After executing one operation, it will automatically return to Insert Mode.

Command-line Mode

Press : in Command Mode to enter Command-line Mode.

In Command-line Mode, you can enter commands consisting of one or more characters. There are many available commands.

In Command-line Mode, the basic commands are:

  • :help/:h View the English Vim online help documentation.
  • :w Save the file.
  • :q Quit Vim.
  • :wq Save the file and quit Vim.
  • :q!/:!q Force quit Vim without saving changes.
  • :e filename Can open the specified file in the current directory.
  • The :s command is for substitution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
" Replace the first occurrence of str1 with str2 on the current line
:s/str1/str2/
" Replace all occurrences of str1 with str2 on the current line
:s/str1/str2/g
" Replace all occurrences of str1 with str2 on the current line, asking before each replacement
:s/str1/str2/gc
" From line x1 to x2, replace the first occurrence of str1 with str2 on each line
:x1,x2 s/str1/str2/
" From line x1 to x2, replace all occurrences of str1 with str2 on each line
:x1,x2 s/str1/str2/g
" From line x1 to x2, replace all occurrences of str1 with str2 on each line, asking before each replacement
:x1,x2 s/str1/str2/gc
" Replace the first occurrence of str1 with str2 on all lines
:%s/str1/str2/
" Replace all occurrences of str1 with str2 in the entire file
:%s/str1/str2/g
" Replace all occurrences of str1 with str2 in the entire file, asking before each replacement
:%s/str1/str2/gc

If the command is in the form :! command, the command will be executed in the bash terminal.

Press Esc to exit Command-line Mode.

Visual Mode

Press v to enter Visual Mode, often used for selecting areas. Press V (Shift+v) to enter Visual Line Mode, used for selecting lines.

Press Ctrl+v or Ctrl+q to enter Visual Block Mode.

After entering Visual Block Mode, press I or A to enter Insert Mode (equivalent to i and a). After exiting Insert Mode, the changes made to the current line will be applied to the same position on every selected line. Often used for batch adding comments.

After selection, typing y or d can also execute the corresponding commands.

The three Visual Modes can be converted between each other via key presses.

Vim Shortcuts

Refer to The Most Comprehensive Vim Shortcut Keymap—From Beginner to Advanced

Advanced Knowledge

The . Command

Vim users inevitably resist repetitive text modifications because Vim inherently requires two extra keystrokes compared to other editors—Esc and i. However, Vim actually provides the repeat command ., which is suitable for repetitive text addition, modification, and deletion operations.

The . command can repeat the last executed command. But this "command" is not limited to a single command; it can also be a combination of number + command; entering Insert Mode + inputting text + Esc is also a type of command. Therefore, using the . command appropriately is key to achieving maximum efficiency.

For example, each line of the following code is missing a semicolon at the end:

1
2
3
4
int a, b
cin >> a >> b
cout << a + b
return 0

Using . in combination with the command A (move to end of line and insert) allows for efficiently adding the missing semicolons.

1
2
3
A;<Esc>
" Repeat the following command
j.

Another example: In the following code, the array names in the last five assignment statements are all incorrect:

1
2
3
4
5
int check() {
  book[1] = 1, book[2] = 1, book[3] = 1, bok[1] = 1, bok[2] = 1, bok[3] = 1,
  bok[4] = 1, bok[5] = 1;
  return 0;
}

Changing them one by one is too tedious, and the s command in Command-line Mode would change them all.

The first method is to use it with the s command in Normal Mode (delete the character under the cursor and enter Insert Mode). Go to the first letter of the first incorrect array name, press 3s/cw, input the correct array name, and exit. Then move the cursor to each subsequent one and use the . command.

The second, more time-saving method is to use search mode for modification. Type /bok, press Enter, and use the n key to go to the first letter of the first incorrect array name. Type 3s new_array_name <Esc>, and finally repeat n..

The third method uses the simple search command f. In Normal Mode on a line, f + single character searches for that character in the line and moves the cursor to it; press ; to find the next occurrence, , to find the previous one. So for the code above, just type fb;;;, then enter Insert Mode to modify, and then ;.. This method is suitable when only movement within a line is needed.

Macros

Vim's macro function can repeat arbitrarily long command sequences.

Before using a macro, you need to "record" it, meaning record a series of keystrokes and then replay them, achieving the effect of repetition. Recording is simple: in Normal Mode, type q to start recording. Next, assign a command key for the macro, which can be any of the 26 letters. The bottom left will then show recording @the_letter_you_chose. You can then start recording commands. Similarly, press q in Normal Mode to stop recording.

To use it, press : to enter Command-line Mode, type @the_recorded_letter, and the previously recorded commands will be invoked.

Combining . with macros, i.e., record macro → invoke macro → . repeat command → number + ., can achieve very high efficiency.

The normal Command

This command is related to Normal Mode. Its effect is to repeat a command on specified lines.

Press : to enter Command-line Mode and input the following command:

1
:a,b normal command

Or:

1
:a,b norm command

The meaning of the above command is to execute the command command in Normal Mode on lines a~b.

Since the normal command can be repeated by the . command and is easy to understand, its usage frequency is even higher than that of macros.

Number + . + Macro + normal

The above three commands can be combined. For example:

I downloaded a book, and I need every chapter to become a "heading" to facilitate conversion to formats like mobi or to generate a TOC for navigation. How can I do this?

Here is the process using Vim:

  1. Press / to bring up the search box, enter a regular expression to search.
  2. Use the q command to start recording a macro.
  3. Type the I# command, then press ESC.
  4. Use the q command to stop recording the macro.
  5. Type normal n@letter to go to the next occurrence and repeat the previous step.
  6. Type number + . to repeat multiple times.