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

How do I navigate by screen lines instead of file lines when text is wrapped in Vim?

Answer

g0 and g$

Explanation

When wrap is enabled, long lines span multiple screen rows but remain a single file line. Normal motions like 0, $, j, and k operate on file lines, jumping past the visual rows in between. The g-prefixed screen-line motions let you navigate what you actually see.

How it works

  • g0 — move to the first character of the current screen line (like 0 for file lines)
  • g^ — move to the first non-blank character of the current screen line
  • g$ — move to the last character of the current screen line
  • gj — move down one screen line (equivalent of j for visual rows)
  • gk — move up one screen line

All five are available in Normal and Visual mode.

Example

Consider a single file line that wraps across three screen rows:

This is a very long line that wraps across multiple screen rows in your terminal window when editing

With the cursor at the start of this line:

  • $ jumps all the way to g (end of file line, far off-screen)
  • g$ stops at the last visible character on the current screen row
  • gj moves the cursor down to the next wrapped screen row, staying in the same column

Tips

  • Many users remap j/k to gj/gk in their vimrc to always move by screen line:
    nnoremap j gj
    nnoremap k gk
    
  • These motions are particularly valuable when writing prose with set wrap linebreak
  • In Visual mode, gj/gk extend the selection by screen row rather than file line

Next

How do I create command-line aliases so that typos like W are automatically corrected to w?