Vim Function to Visually Center a Plaintext File
How to center a pane in vim
The idea is to get the content visually centered on the screen. Typically when I am writing non-code text I still use vim – because it’s fantastic no matter what you’re doing with text – and in a fullscreen terminal, because that’s my workflow for everything. I like to keep to a sane margin width as well. Once you are operating like that, it’s natural to want the content centered, rather than off to the left side of the screen.
(we want the white rectangle to appear in the area of the red one)
This is something that vim doesn’t do on its own. There’s probably a plugin for it or something but I like to keep my plugins to a minimum, especially when it’s for something that the software can do entirely on its own without much coercing.
We can open up a new empty buffer in a vertical split using
:vnew. This gets
us closer to moving our content around, but it’ll start on the wrong side (the
empty split will open on the right). Using
:lefta vnew will open the split
‘left and above’, which in the case of being the first split opened will just
put it to the left:
By controlling the width of this left pane we can get the content we’re interested in to the spot we want it:
Manually creating a split and resizing it every time we want to center some content like this is annoying, though. And the whole point of using a programmable editor is to make it bend to our every whim with minimal effort. Thus:
This happens to be my first vimscript function. I never needed to write one until now. It has the benefit of being very simple:
The three lines of the function:
- open a new split on the left (and automatically switch to it)
- switch back to the content pane
- resize the content pane to be 75% of the total screen
The last line of the above snippet just maps the function call to a key – in
Leader - c. You’ll likely need to adjust that mapping as well as
potentially change the resize amount based on how much spacing you want
(maybe you use a different margin than I).
Huge disclaimer: I haven’t yet tested this on a screen size other than the one I just wrote this function and post on… Might be completely broken everywhere but this here laptop!
I’d be happy if any readers leave comments or scoldings on that gist. I’m sure
there are things wrong with it.