How do I keep a horizontal margin between the cursor and the edge of the screen when scrolling sideways?
Answer
set sidescrolloff
Explanation
set sidescrolloff={n} keeps at least n columns of context to the left and right of the cursor when long lines cause the view to scroll horizontally. Without it, the cursor can move right up to the edge of the screen before the view shifts, making it hard to see surrounding context.
How it works
set sidescrolloff=5— keep 5 columns of context on each side when scrolling horizontally- Works alongside
set nowrap(the option only matters when lines are not wrapped) - Analogous to
scrolloff, which does the same vertically
Example
Add to your vimrc:
set nowrap
set sidescrolloff=8
set sidescroll=1
Now when editing a long line without wrapping, the view keeps 8 characters visible ahead of the cursor, so you always have context for what comes next.
Tips
set sidescroll=1makes horizontal scrolling happen one column at a time (smoother than the default)- Without
sidescroll=1, Vim may jump the view in large increments, makingsidescrolloffless useful set scrolloff=5is the vertical equivalent that many Vim users already use —sidescrolloffbrings the same convenience to horizontal navigation