4

I fond out how to resize a split on the fly in Vim but I would like to have a fixed size for some split (e.g.: the NERDTree split should always has the same size).

I am using VimPlug as a plugin manager and when I run a command like :PlugUpdate it opens a split with some size and I want to change that sizeenter image description here

Looks like it takes half of the window

(I am completely new to Vim and I am trying to set my first .vimrc file so if there are some things I should know it would be great to leave them here)

1

1 Answer 1

4

First we need to resize a split window vertically :h :vertical-resize tells us that from the command line mode we can use

:vertical resize 50

Or we could also use :50wincmd | but I find that less readable. The wincmd command allows to execute from the command line the commands relative to windows that you would execute in normal mode with ctrl+wany command. See the list at :h CTRL-W.


Then you need to execute this command for windows containing a buffer with the filetype vim-plug. The filetype is the mechanism which allows Vim to know what is the content of a buffer and to do stuff accordingly (e.g. syntax highlighting, buffer-local options, etc).

To know which filetype we need to target I used :PlugUpdate, put my cursor in the window and use :set filetype?. This returned vim-plug

There are several ways to do it.

The first one, which is the easiest but the less elegant is to put an autocommand in your vimrc:

augroup vimplug
    autocmd!
    autocmd Filetype vim-plug vertical resize 200
augroup END

Autocommands is a mechanism provided by Vim to execute code when some events happen. The list of available events can be found at :h autocmd-events. Here we know that we want to resize the window when the filetype of the buffer it contains is set to vim-plug.

This works but it adds some bloat in your vimrc and it duplicate the built-in behavior of vim. Note that you could argue that we could simply remove the augroup around the autocommand to remove some bloat. That is tempting but it's not a good idea because then you risk to execute several time your autocommand if you source again your vimrc.

The other solution is to create your own ftplugin and to add the command in it. Ftplugins are the built in mechanism which mimics the autocommand event Filetype. To do so, create the file ~/.vim/after/ftplugin/vim-plug.vim and simply add the following line in it:

vertical resize 200

And you should be good to go, next time you use a vim-plug command the window will be resized.


The related help topics are

1
  • 2
    Working amazingly, thanks for the solution and for the info about the ftplugins ! Commented Jun 3, 2020 at 12:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.