vimtricks.wiki Concise Vim tricks, one at a time.

How do I move to the start or end of a displayed (wrapped) line rather than the actual line?

Answer

g0 / g^ / g$

Explanation

When wrap is on and a long line wraps across multiple screen rows, 0 and $ jump to the start/end of the entire logical line (possibly off-screen). The g0, g^, and g$ motions instead move to the start or end of the currently displayed row — what you actually see on screen.

Display line motions

Motion Action
g0 First character of the displayed line (screen column 1)
g^ First non-blank character of the displayed line
g$ Last character of the displayed line
gm Middle of the displayed line (screen-center column)
gj Down one displayed line (not logical line)
gk Up one displayed line (not logical line)

Comparison

With a 200-character line wrapped at 80 columns:

  • $ — jumps to column 200 (end of the logical line, may be two screen rows down)
  • g$ — jumps to column 80 (end of the first visible row on screen)
  • 0 — jumps to column 1 (same as g0 for the first row)
  • If cursor is on the second wrapped row: g0 goes to column 81, 0 goes to column 1

When to use them

Useful whenever you work with long lines that wrap:

  • Prose writing with textwidth=0 (no auto-wrap)
  • Editing minified code
  • Viewing log files with very long entries

Tips

  • Enable display-line navigation globally with remaps in your vimrc:
    noremap j gj
    noremap k gk
    noremap 0 g0
    noremap $ g$
    
    (With a count-aware version: noremap <expr> j v:count ? 'j' : 'gj')
  • :set nowrap makes g$ and $ identical — they only differ when wrapping is on
  • gM does not exist — for screen-center movement use zH/zL to scroll instead

Next

How do I run a search and replace only within a visually selected region?