How to move screen without moving cursor in Vim?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Vim, the cursor position and the viewport are related but not identical. You can scroll the visible window to inspect nearby text while keeping the cursor anchored, which is useful when you want more context without losing your editing position.
Scroll the Window Vertically
The core viewport-only commands are Ctrl-e and Ctrl-y.
These commands move the screen contents rather than deliberately moving the cursor to a new logical position. They are useful for reviewing code above or below the current line during debugging, refactoring, or code review.
By contrast, commands such as j, k, Ctrl-d, and Ctrl-u are primarily navigation commands. They change where the cursor is, even if the screen also shifts.
Reposition the Window Around the Current Cursor Line
Sometimes you do not want gradual scrolling; you want the same cursor line displayed at a different place in the window. That is what zt, zz, and zb are for.
These commands do not move the cursor to a different buffer location. They change where the current line appears in the viewport, which is often faster than repeated Ctrl-e or Ctrl-y taps.
Scroll Horizontally on Long Lines
When line wrapping is disabled, Vim also supports horizontal viewport movement.
For horizontal review to make sense, you usually want:
Without that setting, wrapped lines can make side-to-side movement less predictable.
Use Marks to Keep an Editing Anchor
Even though viewport scrolling can keep the cursor stable, real editing sessions often involve a mix of scrolling and jumping. Marks make it easy to recover your place.
Later, return to it with:
This is especially helpful when you scroll for context, then follow a search result or definition jump, and want to come back exactly where you started.
Tune Scrolling Behavior
A few options make viewport movement more comfortable.
scrolloff keeps a margin of visible lines around the cursor. sidescroll and sidescrolloff influence how horizontal movement behaves on long lines. These are not strictly required for screen-only movement, but they make the window feel less abrupt during navigation.
Persist them in your Vim configuration if you use them often.
Common Pitfalls
- Using cursor movement commands and expecting the cursor to stay anchored.
- Forgetting about
zt,zz, andzb, which often solve the real problem faster than incremental scrolling. - Trying horizontal screen movement while line wrapping is enabled.
- Scrolling for context without leaving a mark before a larger navigation jump.
- Over-customizing default keys and then losing the standard Vim viewport behavior.
Summary
- Use
Ctrl-eandCtrl-yfor vertical viewport movement without intentionally changing the cursor location. - Use
zt,zz, andzbto reposition the current line within the window. - Use
zh,zl,zH, andzLfor horizontal viewport scrolling on unwrapped lines. - Marks help you preserve an editing anchor during exploration.
- Separating screen movement from cursor movement makes Vim navigation much more precise.

