Loading ...
Sorry, an error occurred while loading the content.
 

How to close a window by window number?

Expand Messages
  • Eric Siegel
    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
    Message 1 of 5 , Jul 23, 2013
      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"?

      Eric

      --
      --
      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@....
      For more options, visit https://groups.google.com/groups/opt_out.
    • Gary Johnson
      ... 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
      Message 2 of 5 , Jul 23, 2013
        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. See

        :help :wincmd

        Regards,
        Gary

        --
        --
        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@....
        For more options, visit https://groups.google.com/groups/opt_out.
      • Tim Chase
        ... 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
        Message 3 of 5 , Jul 23, 2013
          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@....
          For more options, visit https://groups.google.com/groups/opt_out.
        • Eric Siegel
          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
          Message 4 of 5 , Jul 23, 2013
            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@....
            For more options, visit https://groups.google.com/groups/opt_out.
          • Charles Campbell
            ... I agree that its inconsistent that wincmd c doesn t accept a count. However, I d expect that, if it did accept a count, that it would simply perform
            Message 5 of 5 , Jul 24, 2013
              Eric Siegel wrote:
              > 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.
              >
              I agree that its inconsistent that wincmd c doesn't accept a count.
              However, I'd expect that, if it did accept a count, that it would simply
              perform [count] window closes, which would not be the same as closing
              the window with winnr() of [count].
              As far as window numbers changing after the close is performed - that's
              what I'd expect.
              At least w:vars seem to stick with the logical window that they were
              created in.

              Regards,
              Chip Campbell

              --
              --
              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@....
              For more options, visit https://groups.google.com/groups/opt_out.
            Your message has been successfully submitted and would be delivered to recipients shortly.