background

Coding often involves jumping from one file to another or across files, usually using a mouse or shortcut keys (VSCode and WebStorm are commonly used in the front end). But even with Vim, why not use Vim’s jump feature for more precise jumps to improve coding speed and sense of control?

Our command scenario below is in Vim normal mode.

I. Jump within file

Action command jump

Simple words and in-line jumps will not be described, please refer to the table, here are a few common commands:

  • % – Match () [] {} jump
  • F/f – Forward/back matching character jump, the same character repeat forward/reverse matching jump can be used; /,
  • H/M/L – Jumps to the top/middle/bottom of the edit view area
  • Gg /G – jumps to the top/bottom of the entire edit area, and 10gg jumps to line 10

Mark position jump

marks

Type :marks to view the historical marker location of the record

Manually tag

For example, press MM first, then move the jump cursor randomly, then press ‘M’ to return to the previous marked position. M can be replaced with a legal character (note: using uppercase letters creates cross-file marks).

Automatic marking

In addition to recording manually marked positions, there are also automatic marked ones, as follows:The two most commonly used auto-mark locations are:

  • – Toggles between the current position and the last jump position
  • ‘. – Jumps to last modified position

supplement

  • A jump is defined, for example, a small left-right up and down switch is not a jump, so the cursor position switch is not recorded in the tag list, only the cursor movement caused by a large range of action command is recorded

2. Jump between files

In addition to in-file jumps, Vim also supports powerful file-to-file jumps. Let’s learn some common file-to-file jumps techniques.

jumps

Vim maintains a list of jump histories, which can be read using :jumps. CTRL + O and CTRL + I can switch back and forth in the jump history, without modifying the history, and can jump freely between files and within files.

changes

When a change is made to a document, the location of the change is recorded in the change list, which can be viewed using: Changes. You can use g; /g, command for forward/reverse traversal of the change list.

g; `. U < c-r > contrast

In addition to the above mentioned ‘. Can jump to the last modified position, using the side effect of U can also be achieved: press U to undo the last modified position, at this time our position has switched to the last modified position, just press CTRL + R can cancel the u retraction.

‘. Similar to an anchor point, using g; When constantly jumping through change history, you can always use ‘. ‘to return to the last change, and u seems to be the same as’.’ The function is the same, but there are two disadvantages are not recommended to use: one is more keys if the key after U is not effective easy to modify the document; Second, if the change is a new line, u will cancel this line, which is above the target position, resulting in inaccurate positioning.

gi

When we exit edit mode with the cursor hovering around, and then want to edit again, GI can jump to the exit edit position and switch to edit mode (using the position marked automatically by ‘^).

Third, summary

Summary:

  • Use m* + ‘* to mark jumps
  • Use < c-O > + < c-i > to traverse the jump list
  • Use the g; + g, iterate over the change list (the measured ideaVim cannot use g; /g, which VSCode can use but cannot query :changes)

In fact, Vim jump is nothing more than a combination of imperative jump instructions + jump instructions that reuse positions through lists. As long as we can classify the instructions well and deepen our muscle memory, we will be able to code more happily.

reference

Vim Practical Tips (2nd edition)

The original address