1

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!

Anton Tretyakov
  • 303
  • 1
  • 9

1 Answers1

1

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • This is indeed a clever solution I would never think of! The only problem that comes to mind is when some other person having a similar hacky mind tries to use my script (just imagining), things go boom. – Anton Tretyakov May 24 '21 at 20:04
  • 1
    Well, if you want to turn that thing into a plugin for others to use, you will certainly have to put at least *some* features behind options. – romainl May 24 '21 at 20:12
  • I slept on this solution, and now it seems to me that this one and cmdlineleave event solution are in fact similar. The latter seems to be a bit more convenient for me as it will certainly not overwrite potential user's mappings, even though it doesn't cover all the potential events of windows resizing. – Anton Tretyakov May 25 '21 at 13:05
  • `CmdLineLeave` doesn't help because it is also triggered by `` and ``. As for the other ways of resizing windows, you could try to override the default commands like `+`. Another option would be to poll the dimensions of each window with `:help timer` and `:help getwininfo()` and react accordingly. – romainl May 25 '21 at 13:30
  • Indeed, but sifting a needed command (say, "resize") in handler func would do the work (pretty much like in your example). Though, once again, it seems like it causes a big overhead to call the func every time only to check for one command. Timer is a great option, thank you! But in my case i would need to set a timer to quite a small period. This is weird btw that VIm doesn't have a build in event, considering it clearly knows all windows' relevant dimensions at a given time. – Anton Tretyakov May 25 '21 at 15:17