7

If I press zz in vim, my screen/view will center vertically on the cursor position.

How can I do the same, but horizontally? Is there a vim command for that?

3
  • Do you mean moving the cursor to the middle of the current line?
    – Kusalananda
    May 6, 2020 at 20:50
  • @Kusalananda i mean moving the view so that the cursor is at the center of the screen, without changing the cursors actual position
    – isocseles
    May 6, 2020 at 21:09
  • 1
    How would that work if the current line is empty, for example?
    – Kusalananda
    May 6, 2020 at 22:19

2 Answers 2

12

There's no single Vim command, but you can combine zs with zH: Scroll to position the cursor at the left side of the screen, then scroll half a screenwidth to the right.

I have this mapping in my ~/.vimrc:

" Horizontally center cursor position.
" Does not move the cursor itself (except for 'sidescrolloff' at the window
" border).
nnoremap <silent> z. :<C-u>normal! zszH<CR>
1
  • Thanks, I came here looking for zs. E.g., in IdeaVim, when using using n to go to a search term on a long line that's off screen to the right; zs on its own brings it onto the screen. In regular vim I use soft wrap so it's not an issue. Nov 16 at 4:19
2

This isn't a direct answer to the question, but potentially a different solution to the underlying problem. In my case, the problem was for really long lines, the right side falls off the screen and the cursor ends up far on the right side.

:set sidescrolloff=<number of characters>

Interestingly, if you just put a giant number in here, it appears to cap it up to the middle of your screen. In this way, you never have to do a "zz" equivalent because the cursor is always centered horizontally on the screen (except when you're hitting the left edge).

Just for completeness, if you wanted the equivalent vertically, you use

:set scrolloff=<number of lines>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.