Changing Vim's window size

I use Vim in two modes:

  1. When I'm properly stuck into writing code and want both my tests and code visible at the same time, and
  2. When I'm editing some copy, or doing some lightweight hacking, and only need one buffer to be visible at once. Under these circumstances I like a nice tidy window, that doesn't take up half my screen.

In the first case I want a vertical split, with two 80 column wide buffers, both showing plenty of lines of text. In the second I want a single 80 column wide buffer, which rarely needs to be as tall (I don't need as much context).

Picking up the corner of the window with the mouse and painstakingly dragging it to the correct size is annoying, especially when you're trying to hit the right width to show 80 columns of text.

I needed a keybinding. In fact, I needed a couple.

I added these to my ~/.gvimrc file:

nmap <leader>1 :set lines=40 columns=85<CR><C-w>o
nmap <leader>2 :set lines=50 columns=171<CR><C-w>v

I can now type ,1 (I've set my leader key to be a comma) and ,2 to switch quickly between a window with 81 columns (plus 4 for line numbers) and one buffer, or a larger window with space for two 81 column buffers, their line numbers, and the split boundary.

If you're wondering why I set my buffers to 81 columns wide, instead of 80, it's because I like to highlight the 81st column (to warn me not to use it) but can still see that there's nothing in it. If you have Vim 7.3 or later, you can do that with colorcolumn:

set colorcolumn=81

And if you're wondering why all these commands are in .gvimrc rather than .vimrc, it's because this file is only parsed when Vim runs with a GUI. These mappings wouldn't make sense without a GUI.

Published on in Vim