Want to see the full-length video right now for free?
Motions are used to get your cursor where you want it.
As programmers, we tend to spend most of our time editing existing code. This means we spend a lot of time navigating files and positioning the cursor where we want it, and not much time simply typing new code. Fortunately, Vim is optimized for this situation and has many ways to efficiently move your cursor where you want it.
Mapping | Summary |
---|---|
h , l |
move left/right by character |
w |
move forward one (w)ord |
b |
move (b)ackward one word |
e |
move forward to the (e)nd a word |
Many of the motions in Vim have inverses that allow you to go back if you go
too far, e.g. w
and b
. This means that you don't have to be perfectly
precise in your motions, but can simply hit w
a few times, realize you've
gone one too far, and use b
to go back.
Note If you find yourself repeating or holding down a motion key to move around, take a second to consider if there is a more efficient motion.
While the motion commands above are very useful, there are a handful of more precise motions that you can use to more directly jump within a line in a single motion.
Mapping | Summary |
---|---|
f<char> |
(f)ind a character forward in a line and move to it |
t<char> |
find a character forward in a line and move un(t)il it (one character before) |
F<char> |
(f)ind a character backward in a line and move to it |
T<char> |
find a character backward in a line and move un(t)il it |
; |
repeat last f , t , F , or T command |
, |
repeat last f , t , F , or T command, but in opposite direction |
The f
, t
, F
, and T
are more powerful than w
and b
as they allow
you to jump to a specific character, regardless of how far that character is
from the cursor.
To compare f
vs t
; f
"Finds" the character, leaving your cursor on it
while t
goes "un*T*il" the character, leaving your cursor just before it.
Similarly, while both both f
and t
target to the right of the cursor, F
and T
will target before the cursor.
Lastly, ;
and ,
complement f
, t
, F
, and T
by repeating or
reversing the last f
, t
, F
, or T
allowing you to hone in on your
target character and freeing you from the need to be perfectly precise in
your usage.
Now that we have an understanding of how to move within a line, let's tackle moving between lines.
Mapping | Summary |
---|---|
j , k |
move up/down one line |
H , M , L |
move (H)igh, (M)iddle, or (L)ow within the viewport |
/ |
search |
n |
repeat last search |
N |
repeat last search in opposite direction |
C-u , C-d |
move (u)p or (d)own |
<NN>G |
(G)o to line number NN (useful if you have a failing test on a given line) |
As with the previous sections we can see that there is a similar pattern to
motions and their inverses. n
moves forward through search results, N
moves backward. C-u
moves up half a page, C-d
down.
Remember, don't worry about learning all of the motions and commands at once, but instead try to regularly bring one or two new commands, motions, etc into your muscle memory.