Zoom current window in Vim

Fri 19 May 2017

A colleague of mine suggested to me to share some tricks I use in my daily Vim workflow; there aren't many, actually...

I normally have just one Vim window open in a tmux panel (I rarely use the GUI); but sometimes splitting comes handy and, even using one of those large monitors so common nowadays, the space is reduced and it's easy to lose the context.

In order to maximize the current window, the :only command exists (mapped by default to <C-w><C-o> and <C-w>o), but it will permanently close all the others (which isn't the real intent most of the times and turns out to be very annoying when typed by accident).

I definitely find more useful to make a temporary zoom, preserving the existing layout for later usage.

There is a plugin for that, but I'm not very keen to use an "extension" for a so simple task; a few lines in .vimrc will suffice, at least for typical use cases (I was inspired by the last reply here):

" ----------------------------------------------------------------------
" Window zooming
"
function! ZoomWin()
        if exists('g:zoomOn') && g:zoomOn
                let g:zoomOn = 0
                tabclose
        else
                let g:zoomOn = 1
                tab split
        endif
endfunction
nnoremap <C-w><C-u> <C-w>o
nnoremap <silent> <C-w><C-o> :call ZoomWin()<CR>
nnoremap <silent> <C-w>o :call ZoomWin()<CR>

First, using a toggling-type function (see g:zoomOn variable) allows to map both zooming and unzooming to the same key sequence, helping your muscle memory learning.

Then, in order to make things consistent, you'll probably need to change the way your tab statusline is shown (either always or never, but not the default, avoiding that annoying windows' movement when repeatedly press <C-w><C-o> if you, like me, don't normally use tabs):

" Never show tab page labels
set showtabline=0

Finally, mapping :only to <C-w><C-u> (here, "u" stands for "unique") and the new function to <C-w><C-o> ("o" for "only", which recalls to me a temporary property) will make things easier to remember.

Now, split a couple of windows (or more) and enjoy your new bindkey-triggered zooming!

A short video, to get the idea:


Comments