1

I use something like zoomwintab to zoom in/out of a single window view, using tabs to simulate the effect.

It exposes a toggle function to zoom in on a single window, and to zoom back out to the orginal layout.

If I'm zoomed in and I quit (rather than toggle), I want to return to the original layout, but with that window removed.

To achieve this, I'm recording the unique window id the zoom originated from,

let origin_win = win_getid()
" ...zoom in... (elided)
let s:origin_wins[win_getid()] = origin_win

and using that to determine which window I need to quit in addition to the current one.

augroup Zoo
  au!
  au QuitPre * if s:zoomed_win_only() | call s:quit_origin() | endif
augroup END

func! s:quit_origin() abort
  let og = s:get_origin()

  if s:exists(og)
    let i = win_getid()
    call win_gotoid(og) | quit
    call win_gotoid(i)
  endif
endf

I'm very happy with it, but I think it could be improved if there was a way to quit a specific window by its id. That way I could avoid the need to switch to the origin window, quit it, and switch back to the zoomed window I'm about to quit.

Is there a cleaner way I might do this?

2

You can retrieve the window number (as opposed to the window ID) and then use wincmd to close:

let winnr = win_id2win(og)
if winnr > 0
    execute winnr.'wincmd c'
endif

Keep in mind this won't work if the window isn't in the current tab page.

1
  • For my use-case, the window is always going to be in a different tab, unfortunately.
    – ivan
    Apr 16 '18 at 3:15
1

Recent vim includes win_execute() which works across tabpages.

call win_execute(og, 'close')

Beware of changes fired by autocmds from the vimhelp:

The window will temporarily be made the current window, without triggering autocommands. When executing {command} autocommands will be triggered, this may have unexpected side effects. Use |:noautocmd| if needed.

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.