Vim
Screen Movement
Cursor Control
Text Editing
Programming Tips

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.

vim
1" scroll the window down by one line
2Ctrl-e
3
4" scroll the window up by one line
5Ctrl-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.

vim
zt   " put the current line at the top of the window
zz   " center the current line
zb   " put the current line at the bottom

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.

vim
1zh   " scroll left one character
2zl   " scroll right one character
3zH   " scroll left half a screen
4zL   " scroll right half a screen

For horizontal review to make sense, you usually want:

vim
:set nowrap

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.

vim
ma   " set mark a

Later, return to it with:

vim
`a

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.

vim
:set scrolloff=5
:set sidescroll=1
:set sidescrolloff=8

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.

vim
1set scrolloff=5
2set sidescroll=1
3set sidescrolloff=8
4set nowrap

Common Pitfalls

  • Using cursor movement commands and expecting the cursor to stay anchored.
  • Forgetting about zt, zz, and zb, 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-e and Ctrl-y for vertical viewport movement without intentionally changing the cursor location.
  • Use zt, zz, and zb to reposition the current line within the window.
  • Use zh, zl, zH, and zL for 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.

Course illustration
Course illustration

All Rights Reserved.