Chapter 6.Table of ContentsCheatsheet

Moving Blazingly Fast With The Core Vim Motions

Motions (as in movements) are how you move around in Vim. They are commands that when typed move the cursor around with high speed and precision. There are many of them, and each one is best suited for different types and lengths of movement. I find they work great in tandem with Visual Studio Code native Go To features like Go To File and Go To Symbol.

Here’s a condensed list of the most useful ones, and when and how to use them:

Move Horizontally Word By Word

Word motions let you jump from word to word in either direction (from left to right or right to left). As such, they allow you to move faster than the basic horizontal motions h and l.

You can use the w (word) command to jump to the beginning of the next word like so:

Word motions. Type w to move word by word

Likewise, you can:

  • Use b (back) to jump to the beginning of a word backwards
  • Use e (end) to jump to the end of a word
  • Use ge to jump to the end of a word backwards

words and WORDs

So w, b, e and ge let you move word by word in Vim. But what is a word exactly? A word in Vim is either:

  1. A sequence of letters, digits and numbers
  2. A sequence of other non-blank characters
these are 4 words
and these below too
,,, ..... ((((( ,.(

But Vim also has the concept of special kinds of words (with letters, digits and numbers) that also include special characters like ., (, {, etc. They are called WORDs in Vim jargon:

word VS WORD

WORDs are particularly helpful to us programmers because code often has a lot of them:

this is a WORD: Iam_A_WORD(WORD)
this function call sum(2,3) is also a WORD
this array [1,2,3,4,5] is a WORD as well

If you want to move WORD by WORD you can use the capitalized equivalents of the motions described earlier (W, B, E, gE).

In general, word motions allow for more precise changes while WORD motions allow for faster movement:

wwww ==> v   v v   v   v
         word. are two words
         word. is one WORD
WWW  ==> ^     ^  ^   ^

Move To A Specific Character

Find character motions let you move horizontally quickly and with high precision:

  • Use f{character} (find) to move to the next occurrence of a character in a line. For instance, f" sends you to the next occurrence of a double quote.
  • If your target is behind the cursor you can use F{character} to find the previous occurrence of a character
Find character motion. Type fchar to find the next occurrence of the char character

We can clearly see how f is faster and more precise than using word motions by pitching one against the other in an example:

f(   ==> v                        v
         const fireball = function(target){
wwww ==> ^     ^        ^ ^       ^

In addition to f Vim also offers the t (until) command:

  • Use t{character} to move the cursor just before the next occurrence of a character (think of t{character} of moving your cursor until that character).
  • Again, you can use T{character} to do the same as t{character} but backwards

If the different between the f and t commands isn’t quite clear yet, here’s an example that compares both of them:

t(   ==> v                       v
         const fireball = function(target){
f(   ==> ^                        ^

t is really useful when you combine motions with operators to perform text changes as you’ll soon discover (for instance, you could delete everything until ( and change it for something else).

After using f{character} you can type ; to go to the next occurrence of the character or , to go to the previous one. You can see the ; and , as commands for repeating the last character search. This is nice because it saves you from typing the same search over and over again:

fdfdfd ==> v   v               v        v
           let damage = weapon.damage * d20();
           let damage = weapon.damage * d20();
fd;;   ==> v   v               v        v

Move Horizontally Extremely

To move extremely horizontally use:

  • 0: Moves to the first character of a line
  • ^: Moves to the first non-blank character of a line
  • $: Moves to the end of a line
  • g_: Moves to the non-blank character at the end of a line

Moving Vertically

Starting from k and j, we move on to a faster way of maneuvering vertically with:

  • } jumps entire paragraphs downwards
  • { similarly but upwards
  • CTRL-D lets you move down half a page by scrolling the page
  • CTRL-U lets you move up half a page also by scrolling

None of these are my favorites but they’ll do for the time being. In the custom mappings chapter, you’ll learn how to create a custom key binding for the ultimate mid-range vertical motion.

High Precision Vertical Motions With Search Pattern

To move vertically even faster when you have a target in mind, your best option is to search for that target with the /{pattern} and ?{pattern} commands:

  • Use /{pattern} to search forward inside a file
  • Use ?{pattern} to search backwards

Where the {pattern} fragment will often be a string literal (the name of a method, class or variable) but can also be a regular expression.

If you’re near your computer try typing a search command right now. You’ll discover that, as you type a search command, any bit of text within a file that matches your pattern is highlighted. As you continue typing the pattern, the highlighted areas will be updated and reflect the new matches. When you find what you are looking for, type <Enter> and your cursor will jump to the first match in the document. There you can perform some editing if you want and later use n to jump to the next match (or N for the previous one). You can think of n as repeating a search.

Vim loves saving you time: Type /<Enter> or ?<Enter> anytime to run the last search (forwards or backwards). Or use * to do a search for the word under the cursor (# to do the same backwards).

Moving Faster With Counts

Counts are numbers which let you multiply the effect of a command. You can use them by prefixing any command with a count like so:

{count}{command}

For instance:

  • 2w allows us to move the cursor 2 words forward.
  • 5j changes your cursor position to 5 lines below.
  • 3; lets you go to the next third occurrence of a character.
  • 2/baby sends you flying to the second occurrence of baby in a document.

In general, use {count}{motion} to multiply a motion {count} times.

Moving Semantically

In addition to the previous motions which don’t really take into account the meaning of your code, Vim offers additional bindings that take your code semantics into consideration:

  • Use gd to jump to definition of whatever is under your cursor.
  • Use gf to jump to a file in an import.

And Some More Nifty Core Motions

These are yet more motions that can come in handy from time to time:

  • Type gg to go to the top of the file.
  • Use {line}gg to go to a specific line.
  • Use G to go to the end of the file.
  • Type % jump to matching ({[]}).

Summary

Motions are commands that let you move around in Vim with high speed and precision. They are composed of one or more keys typed as melodies. They let you perform different types of movements with various lengths and degrees of precision.

The most basic ones are hjkl with are great for small corrections. Then we have word and paragraph motions (w, {) that are nice for browsing code faster. After those, we have high precision search-like motions (f, /) that let us quickly teleport to a character or a search pattern.

Find character and search motions have repeaters (n, ;) that let us repeat the last search by typing just one character. We can use them to jump from match to match in either direction very quickly. The concept of repeaters is a common theme in Vim and you’ll learn many of them throughout the book. Train yourself to rely on repeaters, and you’ll become the more effective for it.

You can combine counts with motions for greater effect. Our brains are slow at counting so you should limit the use of counts only to short jumps. When using counts with vertical motions it is nice to enable relative numbers because they give you a clear reference to your target. Consider enabling relative numbers if you haven’t already.

Now let’s move onto another foundational block in Vim that will allow you to edit text like if by art of magic: Operators.


Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García