Tuesday, July 23, 2013

Re: How to close a window by window number?

Awesome,

Your fix works mostly. The only problem is that the window numbers change after you perform the close. I simply kept the original buffer name around and checked its window after the close.

Thanks everyone for the help!
I can't believe how inconsistent vimscript can be.
We need a standard library.

Eric

On Tuesday, July 23, 2013 5:20:27 PM UTC-7, Tim Chase wrote:
> On 2013-07-23 17:01, Gary Johnson wrote:
>
> > On 2013-07-23, Eric Siegel wrote:
>
> > > Hi everyone,
>
> > >
>
> > > I'm attempting to write a small function that toggles a specific
>
> > > buffer. By toggle, I mean quits the window and then splits to
>
> > > reopen it. It is important that I quit the window and not delete
>
> > > the buffer.
>
> > >
>
> > > I can't seem to find a vim function that allows me to quit a
>
> > > specific window. I have both the buffer name and the window
>
> > > number.
>
> > >
>
> > > Do I need to iterate over all windows using "wincmd"?
>
> >
>
> > If you have the window number in 'winnr', you can jump to it with
>
> >
>
> > exe winnr . "wincmd w"
>
> >
>
> > or maybe
>
> >
>
> > exe winnr . "wincmd c"
>
> >
>
> > to close it. I haven't tried the last.
>
>
>
> It does seem a bit anomalous that the last one doesn't work, as most
>
> of the other wincmd accept a prefixed count to specify the window on
>
> which it should act (or the number of windows to traverse).
>
>
>
> So you'd have to do a combination of "go there, then close":
>
>
>
> :exe winnr . "wincmd w" | wincmd c
>
>
>
> This does have the potential to leave you in different window, so you
>
> might want to preserve your current window, jump to the one you want
>
> to close, close it, then jump back to the original. Something like
>
> this (untested) mess:
>
>
>
> :let g:oldwinnr=winnr() | exe winnr . "wincmd w" | wincmd c |
>
> exe g:oldwinnr . "wincmd w" | unlet g:oldwinnr
>
>
>
> Ugly, but functional.
>
>
>
> -tim

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment