4

There is a (moderately complicated) way to make an autocommand that only runs when windows are created, but I want to have one for when they are removed as well.

The reason is that whenever a window is created or removed I'd like to run a function I wrote which calculates window heights and re-assigns them into the open windows.

It's mainly about efficient allocation of screen space (tiling). When you remove a window, or add one, any small files may get stretched out into the average height and then it is a pain to shrink them down again.

I don't know that there's a way to embed on this site, so here is gfy link.

https://gfycat.com/UnrulyLikelyDrever

The 9 line file at the bottom ends up taking up half the space once the middle window is closed.

My routine automates it a bit so it works sort of like a tiling window system, but since vimscript is missing some API that would make things nicer it is somewhat slow to run (as I have to use wincmd j/k to move around a bunch inside the function). Therefore I'd like to trigger this only when necessary, which means only when creating or removing entire windows (not upon entry to or exiting focus from them).

Indeed the linked solution for a window open autocommand doesn't even work properly, as I'd really need to have it run after the buffer has loaded (to know the number of lines in the buffer), but I am willing to accept a half-baked solution at this stage.

So the point is I need some autocommands that effectively let me find out when window reflow has occurred. Since there's clearly no WindowReflowed autocommand for this I am looking for some hacks.

  • I think I can fix both of my issues (1. aucmd for new window runs too early before the buffer loads, 2. no aucmd for closing a window) by hooking WinEnter and maybe BufEnter, and using some tab/global variables to latch in order to only run my routine when it is the right time to run it. – Steven Lu Apr 2 '16 at 21:57
  • 1
    Rather than hacking around with VimScript, this sounds like the sort of thing that's best fixed with a patch to create new autocommands. Here's a basic version I quickly created, which seems to work (but isn't tested extensively!) I don't use windows much, so I don't have much incentive to finish this up. The code is public domain, so anyone is free to <s>steal</s> use it and finish it up ;-) – Martin Tournoij Apr 3 '16 at 0:25
  • Well, I think that once i figure out the behavior/algorithms that make the most sense for the window adjustment, a nice next step is to possibly integrate it into vim itself. I feel like just adding a new autocommand only gets me maybe 20% of the way to what I am envisioning anyway. – Steven Lu Apr 3 '16 at 0:49
  • @Carpetsmoker Really do appreciate you sharing that patch though! Now I'll know exactly where to look when I decide to throw up my hands and implement this. Now, still have to find out where to go to do the same in neovim... – Steven Lu Apr 3 '16 at 2:19
4

You could listen to WinEnter and then compare winnr('$') result to the previous one you could have memorized for the current tab.

If the number increases, it means "new window", a decrease will mean "a window has been closed"

0

I didn't quite understand the functionality you are trying to implement, but I'll try to help anyway.

It's an inefficient way, but you can run your function every time you enter a new window, using the WinEnter event.
You will obviously run your function a lot more than needed, but it will also run it when a split is being opened or closed. If you aren't doing anything too heavy, you won't notice it running every time you enter a new split.

For example, this command will resize all the splits to be equal every time you open/close a split or move to a different split:

au WinEnter * normal ^W=

note that ^W is actually Ctrl-w, inserted by pressing Ctrl-v and then Ctrl-w.

  • Yes you're right that if i just use WinEnter without using the tip I linked that it will trigger when a window is closed because closing one means i enter another one. But the routine I want to run is too slow for me to justify doing this. In particular if I move around a window without resizing or opening or closing any of them, it will also run WinEnter. I want it to run only when a window closes. – Steven Lu Apr 2 '16 at 19:41
  • I agree my method is acceptable only with very fast and minimal commands, like my example. Sorry I can't help more than that, I didn't understand how do you want to change the windows when one is closed – GuySa Apr 2 '16 at 19:45
  • I have added a video to demo what I mean in OP. – Steven Lu Apr 2 '16 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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