Vim advancements – moves and jumps

I’m sure you already know what HJKL means. If not, look here.

hjklIt can also be faster!

I’m going to assume (without bias) that you’re using a MacBook. Go to System Preferences > Keyboard and set both Key Repeat and Pre-Repeat delay to maximum.

“Key repetition” determines how fast the system will retype a key when you hold it too long, and “pre-repetition delay” determines how long after you hold it too long the system will retype. Set both options to fastest to make the cursor move faster.

But simply pressing these four buttons to move is a very rudimentary, inefficient operation that should be avoided. If you decide to use Vim for text editing and move the cursor this way, the public will hate you, and your friends and family will laugh at you and spit on your source.

What is a screen row

If you are using the Vim mode of a modern IDE, there should already be line numbers on the left side of the screen. If you are using Vim on a terminal, you may need the set number command to turn on the line number display.

As shown, the cursor is now at _N_isi at the beginning of line 3, at which point we press J and the cursor moves to line 4 instead of qui below Nisi. That’s because JK moves by the actual line, the actual line of text in the document. When the length of a line of text exceeds the width of the window, Vim folds the line. A line in a file may appear as several lines on the screen.

To move up and down the screen line in Vim (in this case from _N_isi to qui), use the gj and gk commands.

The 0 and $commands to move to the beginning and end of a line also have screen lines and actual lines. If you move to the beginning and end of a screen line, you can use g0 and g$.

Based on word movement

The command Mobile location
w Go to the beginning of the next word – forward / word
b To the beginning of the current word, the last word –backward
e To the end of the current word, the next word
ge Go to the end of the last word

Ea can be linked together to “move to the end of the current word and insert after it”, remember that, use it frequently.

To find the character

F{char}, F{char}, T{char}, and T{char} are the fastest ways to find/move in Vim. They can be remembered as find and till respectively.

Where, f{char} represents moving the cursor to the next {char} on the current line, and t represents moving the cursor to the first character of the next {char} on the current line. The uppercase F{char} and T{char} corresponding to the two commands respectively represent forward search.

For example, fo stands for moving the cursor to the next O on the current line.

Instead of pressing fo again to find the next o, the second o, and the third O, Vim gives us the command to repeat the same step: I’m looking down, I’m looking up.

These commands are not only useful for moving the cursor, but also work well with D delete, C change, V Visual, and Y yank.

For example, DTN can be understood as delete from the current cursor to the next N, and can be simply remembered as delete till n. CTM can be understood as “delete from the current cursor to the next M and enter insert mode”, i.e. “change till M”. Vfa, select from current cursor position to next A, yFA, copy from current cursor position to next A.

In practice, tT is preferred when used with D and C, and fF is preferred when used with other moves or with V and Y.

However, fFtF has a limitation: it can only look for one character, and only on the current line.

The command Mobile location
fF{char} Move to up/next{char}
tT{char} Move to up/next{char}The first/last character of
. ; Move forward/back

Cross-line text lookup

In Vim, you can use /{char}⏎ for a multi-character, cross-line lookup, similar to the effect of pressing CMD + f followed by {char} and press Enter in the IDE.

The preceding/following command that matches it is N/ N (next), uppercase forward, lowercase backward.

tag

Sometimes, we might have to jump back and forth between two locations. In this case, the most convenient way is to use “tags”.

M {a-za-z} marks the current cursor position with the letter immediately following the input, for example, MA marks the current position as a. Once marked, when we move to another location, it only takes two keys to jump back.

There are two ways to jump back. In the case of the tag A, ‘A’ jumps to the first non-whitespace character in the line of the tag, whereas’ A ‘jumps not only to the line of the tag, but also to the exact location of the tag, the column of the tag.

Most of the time, you can easily jump back and forth by using mm and ‘m’ repeatedly.

Vim also provides some default flags to jump to, as shown in the following table:

The command Jump to
` ` Position of the current file before the last jump action
`. Where it was modified last time
` ^ Where I inserted it last time
` [ The starting position of the last modification or replication
`] The end of the last modification or copy
` < The starting position of the last highlighted selection
` > The end position of the last highlighted selection

Jump and check in parentheses

There are often parentheses, Angle brackets, square brackets, quotes, HTML tags, etc. Vim also has a series of commands that operate on the text associated with parentheses and so on. In Vim, we refer to these commands as “text objects”.

A text object consists of two characters, the first character always being either I or A. You can think of I as inner or inside, and a as around. Here are some examples of how text objects can be used.

The command The selected range
vi] Select the inside surrounded by square brackets
va] I’m gonna check the square brackets,includingThe square brackets
vi" Select the interior surrounded by double quotes
va" Select the content surrounded by double quotation marks,includingDouble quotation marks
vat Select a pair of HTML tags
vit Select the inside of the HTML tag

vi" vs va"

Similarly, text objects can be manipulated with d delete, c change, or y yank commands, such as da” – delete around a pair of double quotes, cit – modify the content within a pair of HTML tags.

I and a can also be used with the w mentioned above, for example, diw deletes the current word, diw deletes the current word and the space after it, and c, v, and y are similar.

Jump between parentheses

The % command allows us to jump back and forth between matching parentheses. Instead of manually specifying which bracket to jump to, this command automatically jumps to the bracket above the current cursor position.