How do I control how many columns Vim scrolls horizontally at once when the cursor reaches the screen edge?
Answer
:set sidescroll=1
Explanation
When wrap is off and the cursor moves past the edge of the screen, Vim jumps the view horizontally by a number of columns determined by sidescroll. Setting it to 1 gives the smoothest one-column-at-a-time scrolling. The default value of 0 jumps by half the screen width, which can be disorienting on wide files.
How it works
:set nowrap— required to enable horizontal scrolling (wrap mode keeps all content visible)sidescroll=0— the default; when the cursor hits the edge, the view jumps by half the window widthsidescroll=1— the view scrolls smoothly one column at a time as the cursor moves- Higher values increase the jump size per scroll step
Example
For smooth horizontal scrolling when editing long lines:
:set nowrap
:set sidescroll=1
:set sidescrolloff=5
With these settings:
- The view doesn't wrap long lines (
nowrap) - Horizontal scroll advances one column at a time (
sidescroll=1) - The cursor always stays at least 5 columns from the left or right edge (
sidescrolloff=5)
Tips
- Pair with
sidescrolloffto keep a margin between the cursor and screen edge, preventing the view from repositioning exactly at the edge - Use
zLandzHto manually scroll the view left/right by half the window width without moving the cursor - Use
zlandzhto scroll one column at a time without moving the cursor - To persist the setting, add it to your
vimrc