vimtricks.wiki Concise Vim tricks, one at a time.

How do I center the screen on my cursor without leaving insert mode?

Answer

<C-o>zz

Explanation

When you are typing in insert mode and the cursor drifts near the top or bottom of the screen, you normally have to press <Esc>, then zz, then i or a to continue editing. The <C-o> key in insert mode lets you skip that round trip entirely by executing a single normal mode command and returning to insert mode automatically.

How it works

  • <C-o> — while in insert mode, temporarily switch to normal mode for one command
  • zz — center the current line vertically in the window

After zz executes, Vim drops you right back into insert mode at the exact same cursor position. Your typing flow is never interrupted.

Example

You are editing a long file and your cursor has scrolled to the very bottom of the visible screen:

(line 1 visible at top)
...
(line 40 visible at bottom) █ cursor here, barely visible

Press <C-o>zz while still in insert mode:

...
(line 20 now at top)
(line 40 now centered) █ cursor here, centered on screen
(line 60 now at bottom)
...

Tips

  • Use <C-o>zt to scroll the cursor line to the top of the screen from insert mode
  • Use <C-o>zb to scroll the cursor line to the bottom of the screen
  • <C-o> works with any single normal mode command — for example, <C-o>dd deletes the current line without leaving insert mode

Next

How do I use PCRE-style regex in Vim without escaping every special character?