Cover image for Easily center content in vim

Easily center content in vim

vinneycavallo profile image vinney cavallo ・1 min read

Finding your center

vim centering gif

This was originally posted on my programming blog.

I use vim for absolutely everything that I reasonably can. When editing code I typically have a vertical split with files open in both panes - so there is content across the whole screen. But when I'm writing non-code text I usually only have one file open and I like to keep an 80-column margin. This means I have a relatively narrow column of writing all the way on the left side of the screen and it feels and looks weird.

I like to get it centered left-to-right on my full-screen vim window. This is how I accomplish that.

Check out the original post to learn more. For the impatient, here's a snippet:

" centers the current pane as the middle 2 of 4 imaginary columns
" should be called in a window with a single pane

 function CenterPane()
   lefta vnew
   wincmd w
   exec 'vertical resize '. string(&columns * 0.75)

" optionally map it to a key:
" nnoremap <leader>c :call CenterPane()<cr>

Let me know if you find it to be broken in some situations or think it can be improved. Or better yet, if you found a better way to do the same!

Posted on Mar 28 '17 by:

vinneycavallo profile

vinney cavallo


I'm a self-employed developer who doesn't really like primarily defining himself by his income source. Check out my website and other stuff to get a better - but still woefully incomplete - idea of who I am as a person.


markdown guide

That's a great little piece of .vimrc that substitutes Goyo. On the other hand, as soon as you feel like making it more complicated, like, adding another window on the top and on the bottom and hiding the existence of the phantom windows, then Goyo is a better solution.


Ah, I've never heard of Goyo - thanks! That looks great. (along with Limelight, maybe :) )


To center my window with iTerm2, I split my terminal with Cmd+D. I find that way of doing it more useful. Because I usualy always need to use the command line at some point.


I use tmux for splits and panes (and more) in my terminal. If you haven't looked into it I'd recommend checking it out.

One downside I could see to creating this very narrow, centered experience by using terminal splits (rather than doing it in vim) is that everything else you'd be doing in vim would be constrained to that narrow pane as well - like anything in the vim command line, or the quickfix window or anything else.

Another sort of tangential but maybe helpful thing: if you find yourself needing to use the command line briefly but don't want to open up a terminal split you can hit Ctrl-Z to 'background' the current process (vim, in this case), use the prompt as much as you need and then enter the command fg to foreground the last process again (vim). While you're operating in this backgrounded mode you can enter jobs to see the list of backgrounded processes.

I hope that helps!