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 (like0for file lines)g^— move to the first non-blank character of the current screen lineg$— move to the last character of the current screen linegj— move down one screen line (equivalent ofjfor 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 tog(end of file line, far off-screen)g$stops at the last visible character on the current screen rowgjmoves the cursor down to the next wrapped screen row, staying in the same column
Tips
- Many users remap
j/ktogj/gkin 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/gkextend the selection by screen row rather than file line