I sit next to Ben and Mike, two great Vim users, and I learned a lot just from being around them. I’ve said before that I could probably sell our conversations as an advanced Vim book. Movements are just one of the concepts that has improved my Vimming. Note that in my Vim examples, I’m performing each motion with my cursor on the beginning of the phrase.
Movements are a key part of using Vim effectively. Vim commands function as
simple phrases, e.g. “change this word” or “delete until end of line”. I knew
the verbs, but didn’t know how to complete the phrase:
current line) and
yy (yank current line) were my mainstays. To
delete 4 characters, I’d hit
x 4 times in a row. Turns out
there’s a much better way: motions!
y are the verbs: “delete” and “yank”. They need to know
what to delete or yank. And that’s where motions come in. They offer
fine-grained control over verbs so you can yank a single word, or an entire
paragraph (or method block).
f are two of my most frequently used motions.
t means “‘til” and
performs the action 'til a given character. So
dtl turns “hi darling”
f can be thought of as “first occurrence”. It deletes up to
and including a given character, so one character more than “t”. So
hi darling into
F do the same thing as
except backwards. So
dTa deletes backwards til
backwards up to and including the
I recently learned about the even more useful
“inside” and unsurprisingly it moves inside characters. For example, given
di( from anywhere inside the parentheses deletes inside
the parentheses and gives you
(). It erases the text inside the parentheses,
but not the parentheses themselves.
a means “around” and it’s kind of like the
t, in that
includes a bit more characters than “i” does. So
da( changes “(hello world)”
into “” (an empty string). To delete a paragraph, use
dap. In code, a
paragraph means a method definition - so you can delete entire method
definitions with three keys!
For more on motions, check out the official documentation:
It can tell you about the motions (like
a) as well as the “noun”, like the
And don’t forget about the first Boston Vim Meetup on October 24th!