Skip to content

08.Navigate Inside Files with Motions

47. Keep Your Fingers on the Home Row

Vim is optimized for the touch typist. Learn to move around without removing your hands from the home row, and you'll be able to operate Vim quicker.

48. Distinguish Between Real Lines and Display Lines

Avoid frustration by learning the difference between real lines and disply lines. Vim lets us operate on both.

  • The j and k commands move down and up by real lines
  • The gj and gk commands move down and up by display lines

Note the pattern: j k, 0, and $ all interact with real lines, while prefixing any of these with g tells Vim to act on display lines instead.

49. Move Word-Wise

Vim has two speeds for moving backward and forward word-wise. Both allow for a more rapid traversal than moving by one column at a time.

CommandMove Cursor
wForward to start of next word
bBackward to start of current/previous word
eForward to end of current/next word
geBackward to end of previous word

50. Find by Character

Vim's character search commands allow us to move quickly within a line, and they work beautifully in Operator-Pending mode.

CommandEffect
f{char}Forward to the next occurrence of {char}
F{char}Backward to the previous occurrence of {char}
t{char}Forward to the character before the next occurrence of {char}
T{char}Backward to the character after the previous occurrence of {char}
;Repeat the last character-search command
,Reverse the last character-search command
  1. Don't Throw Away the Reverse Character Search Command
  2. Character Searches Can Include or Exclude the Target
  3. Think Like a Scrabble Player

Capital letters are much rarer, and so too are punctuation marks. When using the character search commands, it's better to choose target characters with a low frequency of occurrences.

51. Search to Navigate

The search command allows us to rapidly cover distances both large and small with very few keystrokes.

md
search for your target
it only take a moment
to get where you want

Operate with a Search Motion

md
This phrase takes time but
eventually gets to the point.
  • v/ge<CR>hd
  • d/ge<CR>

Here, we use the /ge<CR> search motion to tell the d{motion} command what to delete. The search commend is and exclusive motion. That means that even though our cursor ends up on the "g" at the start of the word "gets," that character is excluded from the delete operation (see :h exclusive).

52. Trace Your Selection with Precision Text Objects

Text objects allow us to interact with parentheses, quotes, XML tags, and other common patterns that appear in text.

Text objects define regions of text by structure (see :h text-objects).

js
var tpl = ['<a href="{url}">{title}></a>'];
Text ObjectSelectionText ObjectSelection
a) or abA pair of (parentheses)i) or ibInside of (parentheses)
a} or aBA pair of {braces}i} or iBInside of {braces}
a]A pair of [brackets]i]Inside of [brackets]
a>A pair of <angle brackets>i>Inside of <angle brackets>
a'A pair of single quotesi'Inside of single quotes
a"A pair of double quotesi"Inside of double quotes
a`A pair of backticksi`Inside of backticks
atA pair of <xml>tags</xml>itInside of <xml>tags</xml>

Text objects are not motions themselves: we can't use them to navigate around the document. But we can use text objects in Visual mode and in Operator-Pending mode. Remember this: whenever you see {motion} as part of the syntax for a command, you can also user a text object.

If the f{char} and /pattern<CR> commands are like a flying kick to the head, then text objects are like a scissors kick that strikes two targets with a single move.