See
:help motion.txt
for
a complete listing of motions that Vim supports.
Motions can be used for navigating a codebase, but are most powerful when combined with operators that
act on motions. The syntax is [count]<operator>[count]<motion>
. The canonical
example would be to use d2w
to delete two words from the current cursor location. Common
operators are
c
- changed
- deletey
- yankgu
- make lowercasegU
- make uppercasegq
- reflow/format text<
, >
- shift left, right
Operators can also be applied to text-objects like matching quotes,
parentheses, braces, words, sentences, etc. For example, diw
deletes the word under the
cursor without having to move the cursor to the beginning of the word with bde
.
vim-surround also works nicely with text objects and motions. For
example, ys2w'
surrounds the next two words (from the current cursor) with single quotes.
The common motions I use to navigate the current line are
h
, l
- left/right by one character0
, ^
, $
- go to the first, first non-blank, or last character
of a line
[count]|
- go to the count
th columnf{char}
, t{char}
- go to the first occurrence of {char}
.
f
lands on the character, while t
puts the cursor right in front
F{char}
, T{char}
- same as f
and t
, but forward
through the line
[count];
, [count],
repeat the last f
, t
,
F
, or T
command count
times
k
, j
- move up/down[count]-
, [count]+
- (relative) move count
lines up/down[count]G
- (absolute) move to the last character of line count
. Defaults
to last line
[count]gg
- (absolute) move to the first non-blank character of line
count
. Defaults to first line
For lowercase word motions, words are defined as a sequence of leters, digits, and underscores. For uppercase WORD motions, words are separated by whitespace.
w
- move count
words forwarde
- move forward to the end of word count
b
- move backward to the beginning for word count
See text-objects for details. Some text objects can provide motions (paragraphs and sentences)
(
, )
- move count
sentences backwards/forwards{
, }
- move count
paragraphs backwards/forwards]]
- move count
sections forward, or to the next opening {
][
- move count
sections forward, or to the next closing }
[[
- move count
sections backward, or to the previous opening
{
[]
- move count
sections backward, or to the previous closing
}
Some languages have ftplugins that define sections for its particular syntax, so the
]]
-style motions can work for non-C-style languages.
I haven't found the ]]
motions reliable, but that could be because I haven't used them
much.
%
- go to the matching item at or after the cursor. Becomes more powerful with the
matchit plugin
loaded.
[(
, [{
- go to the previous unmatched parenthesis or brace. Useful for
jumping to the beginning of a C-style statement
])
, ]}
- go to the next unmatched parenthesis or brace. Useful for jumping
to the end of a C-style statement
]m
, ]M
- go to the next start/end of a C++-style method[m
, [M
- go to the previous start/end of a C++-style method
Note that %
is a bit funky in HTML. If your cursor is on top of a <
or
>
, it will go to the matching bracket. If the cursor is inside the tag,
%
will go to the matching tag. For lists, %
will cycle through the list
items.
Also note that matchit
doesn't support Python, because of the language syntax.
python_match
exists, but I haven't used it.