Anton Tretyakov
Anton Tretyakov

Reputation: 355

vimscript - catching window resize automatically (with autocommand or not)

So, I was trying to write an autocommand that would fire every time window is resized (not the Vim window entirely, a CTRL-W window).

As there is no dedicated event, i tried using OptionSet with different windows' sizes options, but that obviously does not work for :resize commands and keystrokes, etc.

I was thinking about catching :resize command itself, but it seems like there is no event for doing that (using events for entering a command line seems to be too expensive).

Are there any vim-fu masters who could show me the way? That would be great if there could be a universal way for catching window resizing per se, but other options would do too. Thanks in advance!

Upvotes: 1

Views: 321

Answers (1)

romainl
romainl

Reputation: 196566

The method I used there to perform different actions on <CR> depending on the current command-line could be used here, too:

function! MyCR()
    " grab the current command-line
    let cmdline = getcmdline()

    " does it start with 'resize'?
    if cmdline =~ '^resize'
        " press '<CR>' then do something with that information
        return "\<CR>:echo 'There was an attempt to resize the window.'"
    else
        " press '<CR>'
        return "\<CR>"
    endif
endfunction

" map '<CR>' in command-line mode to execute the function above
cnoremap <expr> <CR> MyCR()

Basically, when you press <CR> to execute an Ex command, do something specific when the command starts with resize and just do a normal <CR> otherwise.

Upvotes: 1

Related Questions