How can Vim be configured to automatically centre a specified line number (eg. without manually having to use the zz command?

So from the commandline I just want to issue:

vim verylongfile.c +932

...and as soon as the file opens, the line is visible in the centre of the screen.

I remember (fondly) this being a default setting on Fedora Core 4's shipped copy of vim.

On my work PC, vim does it the way I want by default. At home, my vim tends to put the line at the bottom of the screen, which gets annoying when wanting to view code and the context around it while flipping between files a lot.

Both machines have the same .vimrc file in the user's home directory. Bothe systems have recent versions of vim (from up-to-date apt and yum repositories).

Is there a magic incantation I can add to the .vimrc to for this apparently optional behaviour all the time by default?

2 Answers 2


Is there a magic incantation I can add to the .vimrc?

Just add this to you vimrc:

Centrum Autem Gravitatis!

Just kidding. ;)

Try this:

autocmd BufEnter * normal! zz

This will add an auto command that will execute zz whenever vim enters a buffer.

  • Interesting. My trusty old .vimrc file has the zz part in the autocmd BufReadPost... section. Perhaps this is something that has changed over the old versions of vim. This works on my work PC, I'll try it on my other PC later today. Cheers.
    – Wossname
    Jun 9, 2017 at 12:17
  • Hmm... Vim says BufEnter happens after BufReadPost. Although I'm not sure why it wouldn't work on your other machine unless it's getting overridden somehow. Curious if this makes a difference for you.
    – Tumbler41
    Jun 9, 2017 at 12:24
  • I think that BufReadPost recall the last line your cursor was on when you exited the file last time (with or without saving -- that's an awesome feature!). It takes you back there if you don't use the vim +123 option. Both my old version and your suggestion work nicely together without seeming to conflict. I'll post my .vimrc file later if it works on my home PC.
    – Wossname
    Jun 9, 2017 at 12:39

I'm not sure why the default +{num} isn't placing the line centre-screen on your home machine, but the + command-line argument can also be used to run commands. Thus, the following invocation should do the trick:

vim verylongfile.c +"normal 932Gzz"
  • That's handy to know.
    – Wossname
    Jun 9, 2017 at 12:42

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.