Software Engineer @ Twitter
A common workflow in vim is to use multiple windows to view and edit various files at the same time. Once opened though, the size of each window often remains unchanged unless explicitly resized.
A nicer workflow would be for the focused window's width to automatically expand.
This behavoir requires surprisingly little vim script and has become one of my favorite additions. It's accomplished with the following snippet that uses an autogroup to call a function on the
augroup ReduceNoise autocmd! " Automatically resize active split to 85 width autocmd WinEnter * :call ResizeSplits() augroup END function! ResizeSplits() set winwidth=85 wincmd = endfunction
The first line of the function sets
to 85. This settings controls the width for the active window; the number represents the number of columns for the active window including the sign and line number columns. If those require 5 columns and
is set to 85, 80 columns are used for the text.
The second line,
, equalizes the width and height for all the other unfocused windows; see
for more information and some other great window commands.
: h wincmd
The above is a slimmed down version of a more practical implementation. Controlling other window settings can reduce additional noise on unfocused windows increasing your signal to noise ratio fo r your active work. Some additional ideas include:
Enabling these settings is easily done by extending the above autogroup to include some additional auto commands like the ones shown below.
autocmd WinEnter * setlocal cursorline autocmd WinEnter * setlocal signcolumn=auto autocmd WinLeave * setlocal nocursorline autocmd WinLeave * setlocal signcolumn=no
When a window is entered, the
are enabled. Unfocused windows disable these settings.
Whatever you do and don't like to see in focused and unfocused windows is now in your control. This can include status lines for active and inactive windows through (each window can have its own status line setup: see an example here in my vimrc).
The autocommands and resizing function work well in most cases, but may have some unintended effects. Windows created by plugins like NERDTree or to display the quick fix and location list may expand unexpectedly when focused.
One resolution to this unintended behvaoir is to add some if statements to the resize function to only execute if the active window's file type is not NERDTree, quick fix, or location list. You may not want
to run when on these windows as it equalizes not just the width but also the height of all open windows.
Despite these (minor) shortcomings (most of which can be accounted for and fixed), this has become one of my favorite and most useful settings. Its a pattern you won't know you missed until you had it.
At least one plugin has been created for this behavior: Lens.vim. If plugins are more appealing to your set up it can support some additional cool features.
Create your free account to unlock your custom reading experience.