How do I make Vim preserve the cursor column when jumping between lines?
Answer
:set nostartofline
Explanation
By default, many Vim movement commands — gg, G, Ctrl-d, Ctrl-u, Ctrl-f, Ctrl-b, and others — snap the cursor to the first non-blank character of the destination line. Setting nostartofline disables this behaviour and keeps the cursor in the same column whenever possible.
How it works
- The
startoflineoption (on by default) causes certain "large" motions to reposition the cursor to column 1 (or the first non-blank character) after the move :set nostartoflineturns this off: the cursor stays in whatever column it was in before the jump, clamped to the length of the destination line- Affected commands include:
gg,G,H,M,L,Ctrl-d,Ctrl-u,Ctrl-f,Ctrl-b,%,{,}, and Ex commands like:N(jump to line N) - Normal word and character motions (
w,b,j,k) are unaffected — they never usedstartoflineanyway
Example
Given this file, with cursor at column 10 on line 1:
col: ^cursor here
short
a longer line with more text
With default startofline, pressing } moves to line 3, column 1.
With nostartofline, pressing } moves to line 3, column 10 (or the end of line 3, whichever is less).
Tips
- Add
set nostartoflineto yourvimrcfor a permanent, column-aware navigation style - This is especially useful when working with structured data (CSV, aligned tables) where staying in a column is important
- Some plugins and Vim internals still reset the column; this option only affects the listed motion commands