DEV Community

Cover image for Moving Blazingly Fast with The Core Vim Motions
Jaime González García
Jaime González García

Posted on • Originally published at

Moving Blazingly Fast with The Core Vim Motions

This is a chapter Moving Blazingly Fast with The Core Vim Motions from the book Boosting Your Coding Fu with VSCode and Vim.

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:

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

Learn Commands With The Help of Mnemonics

Vim has a humongous amount of commands. In time, when you become an experienced Vim user, the commands will become second nature, reflexive and automatic. At the beginning though, you'll need to learn them one by one.

A great way to learn Vim commands is by using mnemonics like associating a letter to a word that is easier to remember. For instance, you can remember that w lets you move from word to word by associating it with word, or that b lets you move word to word backwards by associating it with back.

Following this technique, you'll find that you can learn and recall a lot of commands with little effort.

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
,,, ..... ((((( ,.(
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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  ==> ^     ^  ^   ^
Enter fullscreen mode Exit fullscreen mode

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

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 ==> ^     ^        ^ ^       ^
Enter fullscreen mode Exit fullscreen mode

In addition to f Vim also offers the t (un*t*il) command:

  • Use t{character} to move the cursor just before the next occurrence of a character (think of t{character} of moving your cursor un*t*il 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(   ==> ^                        ^
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

On Notes, Melodies And Chords

Vim is quite special. If you have used other editors you're probably accustomed to typing chords of keys. That is, typing a combination of keys at the same time. For instance, CTRL-C to copy and CTRL-V to paste. Although Vim is no stranger to chords, it relies even more on melodies of keys.

A melody is a series of notes played one after the other. If you think of keys as notes, then melodies of keys are keys that are pressed one after the other in rapid succession. So, when you read that you need to type f{character} to find a character in a line, it means that first you type f, and then you type the character {character}. These two keys are, thus, played as a melody.

Using melodies of keys is the most common way in which you interact with Vim. And, although unfamiliar and kind of strange, it is very convenient because controlling the editor suddenly feels like you're just typing text. It is also marvellous for your wrists' health because you no longer need to rely of complex and unnatural key combinations that strain your poor joints1.

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.

Enabling Highlighted Search

For some reason unbeknownst to me, highlighted search isn't enabled by default in VSCodeVim. To remedy that, go to Preferences and enable the Vim: Hlsearch option (or, alternatively, use the vim.hlsearch option in the json version of the configuration).

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:

Enter fullscreen mode Exit fullscreen mode

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.

Relative Line Numbers

A great way to move vertically within Vim is by using counts in combination with the j and k motions. But sometimes it can be hard to know how many lines you need to jump to get to the desired position. That's where relative line numbers can come in quite handy.

Relative line numbers show line numbers in relation to your current line: If the current line is where your cursor is resting, the line below would have line number 1, the next one line number 2, and so on. Using relative line numbers, it is very straightforward to see how many lines you need to jump to get to where you want to go. Just take a sneak peek at the relative line number and use it as a count.

You can enable relative line numbers in VSCode by going to Preferences (Remember! Use the command palette with CMD-SHIFT-P or CTRL-SHIFT-P), searching for the line numbers configuration and setting it to relative.

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 ({[]}).


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.

  1. This is great if you've experienced carpal tunnel syndrome or other wrist joint problems. Also do yourself a favor and get a nice ergonomic keyboard. You're in this for the long haul. 

Top comments (4)

weeklytyped profile image
Weekly Typed

Thanks for this! I've been trying to start using vim for a while now. One of vim's biggest features (imo) is making it easy and fast to navigate files. I was able to just follow along with a file while reading your article. You've made it clear how to zoom around files in vim!

muhajirdev profile image
Muhammad Muhajir

Check , there are some vscode vim tips. You might like it

vintharas profile image
Jaime González García

Nice! 😁👍

vintharas profile image
Jaime González García

Awesome! Thank you! 😄