Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 ยท Modes and Navigation

Vim Line, Paragraph and File Motions

Move fast across whole lines and files in Vim: 0 ^ $, gg and G with counts, paragraph braces, % for matching pairs, H M L and scrolling keys.

Moving by character and word is the small scale. Real files are long, so you also need motions that cross whole lines, blocks of code and screens. This lesson gives you a toolkit for moving anywhere in a few keystrokes.

Within the line

Key Moves to
0 the very first column
^ the first non-blank character
$ the end of the line
g_ the last non-blank character
| a column number (40| goes to column 40)

Beginners reach for 0, but ^ is almost always what you want in indented code. As motions, they combine nicely: d$ deletes to the end (also written D), c$ is C, y^ yanks back to the first word, and d0 deletes back to column 0.

+ and - go to the first non-blank of the next and previous line; Enter does the same as +. They are quick for stepping through a list of items.

Whole-file jumps

Key Moves to
gg the first line
G the last line
G or gg line number n
:{n} line n (command-line form)
% n percent through the file

50% jumps to the middle. Combined with operators, dG deletes from the current line to the end of the file, dgg to the beginning, and yG copies it all. ggVG selects everything. If you know a line number from a stack trace, :42 jumps there; vim +42 file.py starts there.

Paragraphs and sentences

A paragraph is a block of text separated by blank lines, which matches how most code is spaced into groups.

Key Moves
} to the next blank line
{ to the previous blank line
) to the next sentence start
( to the previous sentence start

In source code } and { are fast block-to-block hops. Try d} to delete the rest of the current paragraph. The operator form dap (delete a paragraph) appears with text objects in chapter 3.

Matching pairs with %

% jumps to the matching bracket: ( ) [ ] { }. On an opening brace it goes to the closing one and back again. With an operator, d% deletes a whole balanced group starting at the cursor. Vim also pairs /* */ and, with the matchit plugin (bundled with Vim as packadd matchit), HTML tags and if/end words. :help % has the full list.

JS Matching brackets, like % (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

This models what % does when the cursor is on a bracket. (Real % also searches forward on the line for the first bracket if you are not on one.)

Screen-relative motions

You can also move by what is visible:

Key Moves to
H top line of the window (High)
M middle line
L bottom line (Low)
Ctrl-d / Ctrl-u half a page down/up
Ctrl-f / Ctrl-b a full page down/up
Ctrl-e / Ctrl-y scroll one line without moving the cursor
zz zt zb scroll so the cursor line is centered, top, bottom

Ctrl-d and Ctrl-u are the most useful; they keep your place in context. After a jump with G or a search, zz centers the line so you can see what surrounds it. zt puts the line at the top, ideal when reading a function.

Jumping to definitions and words under the cursor

A few motions go beyond text structure:

  • * and # search forward or backward for the word under the cursor.
  • gd goes to the local definition of the name under the cursor, a rough approximation that works in simple C-like code without any plugin.
  • gf opens the file whose name is under the cursor.
  • ]] and [[ move between top-level function starts in many languages (they look for a { in column 1; filetypes tune this).

Neovim with a language server provides precise versions of these; chapter 7 covers that.

A line and paragraph model (stand-in)

The block below implements {, }, gg and G-style jumps on an array of lines, to make the blank-line rule concrete. It is a model, not real Vim.

JS Paragraph jumps (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Putting it together

A realistic workflow: you see a function in a stack trace at line 120. Type 120G, then zz to center it. ]] or { finds the start; % jumps from the opening brace to the closing one to find the function's end; V% selects the whole block. That is six keys for what usually takes a mouse drag and scrolling.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which motion goes to the first non-blank character of the line?

    ^ lands on the first non-blank character; 0 goes to column one.
  2. 2. What does 50% do?

    A count before % moves to that percentage of the file.
  3. 3. What does d} do?

    } moves to the next blank line, so d} deletes up to it.
  4. 4. How do you center the cursor line on screen?

    zz centers, zt puts the line at the top, zb at the bottom.
  5. 5. What is Ctrl-d?

    Ctrl-d scrolls down half a page and moves the cursor; Ctrl-u goes up.

Summary

  • 0 ^ $ g_ move within a line; gg G {n}G move across the file.
  • { } hop between paragraphs; % jumps between matching brackets.
  • H M L and Ctrl-d Ctrl-u move around the screen; zz zt zb reposition the view.
  • * # search for the word under the cursor.
  • All of these can follow an operator: d}, y%, dG.

Next: Marks and the Jump List