5 Minutes Vim: Moving Fast Within a File
In this 5 minutes of Vim goodness you’ll learn how to move fast like the wind inside the confines of a file. Here we go!
The Very Basic Motions
hjkl
are the core and most basic motions in Vim. They allow you to move the cursor by one space in every direction:
↑
← h j k l →
↓
They are not the most effective, nor the most efficient way to move around in Vim. But they do give you agility and confidence to move around a file in Normal mode. Learning to hjkl
effectively is the equivalent of learning to walk, or learning to ride a bike.
After you get comfortable with more Vim motions you won’t use hjkl
as much, but they’ll come very handy for short-distance movements and small corrections.
Moving Horizontally
A better way to move horizontally, is to jump around word by word.
w
lets you jump to the beginning of the next worde
moves the cursor to the end of a word and from there to the end of the next wordb
likew
but in the opposite directionge
likee
but in the opposite direction
In Vim not all words are created equal, there’s words, and then there’s WORDS. The difference between words and WORDS is that the former only include letters, digits and numbers.
Vim provides a series of motions that let you jump from WORD to WORD. These are the capitalized versions of the previous motions: Where we used w
we will now use W
, B
instead of b
and so on.
In general, word motions allow for more precise changes while WORD motions allow for faster movement.
Moving Horizontally With High Precision
To move horizontally even faster you can use f
and t
(which have the F
and T
variants to go backwards).
f
lets you find the next occurrence of a character in the line you’re in. f{char}
(as in f:
f.i.) brings you to the next occurrence of that character (:
in our example). The t
motion is similar to f
. The only difference is that t
(think of until) places the cursor just before a character.
Repeating Searches
After using f{char}
you can type ;
to go to the next occurrence or ,
to go to the previous one. You can see the ;
and ,
as commands for repeating the last character search.
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 lineg_
: Moves to the non-blank character at the end of a line
All of these are quite hard to type so try these mappings instead:
" H to move to the first character in a line
noremap H ^
" L to move to the last character in a line
noremap L g_
Moving Vertically
Starting from k
and j
, we move on to a faster way of maneuvering vertically which are {
and }
:
{
jumps entire paragraphs downwards}
similarly but upwards
These two have a couple of disadvantages:
- They are hard to type.
- Moving down a paragraph of code can be unpredictable (it depends on the whitespace)
A better way to move vertically is to scroll up and down by half a page:
C-D
let’s you move down half a pageC-U
let’s you move up half a page
High Precision Motions With Search Pattern
To move vertically even faster when you have a target in mind, your best option is to search with the /{pattern}
and ?{pattern}
commands:
- Use
/{pattern}
to search forward inside a file - Use
?{pattern}
to search backwards
You’ll see that as you type, the matched patterns are highlighted. When you find what you want, type <Enter>
and your cursor will jump to the first match in the search. 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: At any time, you can type /<Enter>
or ?<Enter>
to run the last search (forwards or backwards). Use *
to do a search for the word under the cursor.
Moving Faster With Counts
Counts are numbers which can be prefixed to a command to multiply the effect of that command. For instance, 2w
allows us to move the cursor 2 words forward. Use {count}motion
to multiply a motion {count}
times.
A great way to move vertically is to take advantage of counts in combination with j
and k
. One of my favorite ways to browse a code file is by using 5j
and 5k
. This is much nicer on the eyes than C-d
and C-u
because it moves the cursor instead of scrolling the whole buffer.
Typing 5j
and 5k
can be tedious so use this mapping:
" Move down file lines
noremap J 5j
vnoremap J 5j
" Move up file lines
noremap K 5k
vnoremap K 5k
More Tips!
gg
to go to the top of the file{line}gg
to go to a specific lineG
to go to the end of the file%
jump to matching({[]})
Up Next
Up next, moving between files and more. If you are into meatier articles then check out the exploring vim series or my book Wizards Use Vim.
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.