Vim Tips: Implement your own window zoom
What
Zoom in or out with a single shortcut <C-W>z
. When you navigate into another
buffer such as with <C-w>h
you will unzoom so it doesn’t interfere with your
workflow.
Why
If you’ve incorporated tmux into your
workflow you might have enjoyed the ability to focus a single window with the
built in command <PREFIX>Z
.
For a long time there has been no Vim plugin that mimicks this feature. We can implement it on our own though with a couple lines of viml and no dependencies.
How
Append the following to your Vim configuration.
- vim
1 |
|
Explanation
In order to zoom we use wincmd {arg}
. This is an API to access window commands you would normally execute via <C-w>{arg}
:help wincmd
Like executing CTRL-W [count] {arg}. Example:
:wincmd j
Moves to the window below the current one. This command is useful when a Normal mode cannot be used (for the CursorHold autocommand event). Or when a Normal mode command is inconvenient.
In order to keep track of whether the current window is zoomed we declare a
window-local variable w:zoomed
. This means you can move to other windows or
close the current window without changing the status of other windows.
let w:zoomed
is the declaration and unlet w:zoomed
removes the variable.
The exists({expr})
returns true if the variable is declared.
The two functions WindowZoom()
and WindowUnzoom()
simply delegate their
work to the window command =
which equalizes the space between all windows
thus normalizing. and \
and |
which make the current pane full size.
autocmd
is run on the events WinLeave
and TabLeave
. This causes the
window to be unzoomed if it is zoomed when you navigate away.
Finally we map <C-w>z
to call the WindowZoom()
function. Calling it twice
will unzoom.