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

how to get list of open windows

Expand Messages
  • gmguyx
    Is there some function I can call to get the list of open windows (not buffers) in vim? When I say windows I mean split windows. The reason I need this is
    Message 1 of 8 , Aug 31, 2007
    • 0 Attachment
      Is there some function I can call to get the list of open windows (not
      buffers) in vim? When I say windows I mean split windows. The reason I
      need this is because I'm writing a script whose behavior will be
      different depending on how many split windows are open.


      --~--~---------~--~----~------------~-------~--~----~
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
      -~----------~----~----~----~------~----~------~--~---
    • Tim Chase
      ... If you mean an actual List of the ( :help List ), you can use ... If you want the filename as well, ... With a little jockeying, you can even preserve the
      Message 2 of 8 , Aug 31, 2007
      • 0 Attachment
        > Is there some function I can call to get the list of open windows (not
        > buffers) in vim? When I say windows I mean split windows. The reason I
        > need this is because I'm writing a script whose behavior will be
        > different depending on how many split windows are open.


        If you mean an actual List of the (":help List"), you can use


        :let x=[]
        :windo call add(x, winnr())

        If you want the filename as well,

        :let x=[]
        :windo call add(x, [winnr(), bufname('%')])
        :echo x

        With a little jockeying, you can even preserve the window and
        return to it:

        :let oldwin=winnr()
        :let x=[]
        :windo call add(x, winnr())
        :exec oldwin.'wincmd w'

        HTH,

        -tim



        --~--~---------~--~----~------------~-------~--~----~
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
        -~----------~----~----~----~------~----~------~--~---
      • A.Politz
        ... list_of_open_window = range(1,winnr( $ )) -ap --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist.
        Message 3 of 8 , Aug 31, 2007
        • 0 Attachment
          gmguyx wrote:

          >Is there some function I can call to get the list of open windows (not
          >buffers) in vim? When I say windows I mean split windows. The reason I
          >need this is because I'm writing a script whose behavior will be
          >different depending on how many split windows are open.
          >
          >
          >>
          >
          >
          list_of_open_window = range(1,winnr('$'))

          -ap


          --~--~---------~--~----~------------~-------~--~----~
          You received this message from the "vim_use" maillist.
          For more information, visit http://www.vim.org/maillist.php
          -~----------~----~----~----~------~----~------~--~---
        • gmguyx
          Thanks for the help Tim and AP! --~--~---------~--~----~------------~-------~--~----~ You received this message from the vim_use maillist. For more
          Message 4 of 8 , Aug 31, 2007
          • 0 Attachment
            Thanks for the help Tim and AP!


            --~--~---------~--~----~------------~-------~--~----~
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
            -~----------~----~----~----~------~----~------~--~---
          • Hari Krishna Dara
            ... You shouldn t have to use :windo just to get the list of windows. Vim would actually move the cursor to each window to execute the command. If you are
            Message 5 of 8 , Aug 31, 2007
            • 0 Attachment
              On Fri, 31 Aug 2007 at 7:47pm, Tim Chase wrote:

              >
              >> Is there some function I can call to get the list of open windows (not
              >> buffers) in vim? When I say windows I mean split windows. The reason I
              >> need this is because I'm writing a script whose behavior will be
              >> different depending on how many split windows are open.
              >
              >
              > If you mean an actual List of the (":help List"), you can use
              >
              >
              > :let x=[]
              > :windo call add(x, winnr())
              >
              > If you want the filename as well,
              >
              > :let x=[]
              > :windo call add(x, [winnr(), bufname('%')])
              > :echo x
              >
              > With a little jockeying, you can even preserve the window and
              > return to it:
              >
              > :let oldwin=winnr()
              > :let x=[]
              > :windo call add(x, winnr())
              > :exec oldwin.'wincmd w'

              You shouldn't have to use :windo just to get the list of windows. Vim
              would actually move the cursor to each window to execute the command. If
              you are writing a plugin, I would advise to not do anything that the
              user doesn't expect, such as jumpting to a different window and thus
              triggering the events related to that.

              Actually, the windows are identified by sequential numbers from 1 to
              winnr('$') and they don't have any fixed ids so they are reliable only
              when the window layout has not changed. Like Tim, I am not sure what you
              are trying to do, but here are some simpler equivalents for the above
              suggestions:

              > If you mean an actual List of the (":help List"), you can use
              > :let x=[]
              > :windo call add(x, winnr())

              let x = range(1, winnr('$'))

              > If you want the filename as well,
              > :let x=[]
              > :windo call add(x, [winnr(), bufname('%')])
              > :echo x

              let x = map(range(1, winnr('$')), '[v:val, bufname(winbufnr(v:val))]')

              >
              > With a little jockeying, you can even preserve the window and
              > return to it:
              >
              > :let oldwin=winnr()
              > :let x=[]
              > :windo call add(x, winnr())
              > :exec oldwin.'wincmd w'

              Not required as we are not moving out of the window.


              --
              Thanks,
              Hari

              >
              > HTH,
              >
              > -tim
              >
              >
              >
              > >
              >

              --~--~---------~--~----~------------~-------~--~----~
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
              -~----------~----~----~----~------~----~------~--~---
            • Tony Mechelynck
              ... To determine how many split-windows are open: winnr( $ ) How many in the current tabpage: tabpagewinnr(tabpagenr(), $ ) List all split windows by tab
              Message 6 of 8 , Aug 31, 2007
              • 0 Attachment
                gmguyx wrote:
                > Is there some function I can call to get the list of open windows (not
                > buffers) in vim? When I say windows I mean split windows. The reason I
                > need this is because I'm writing a script whose behavior will be
                > different depending on how many split windows are open.

                To determine how many split-windows are open:

                winnr('$')

                How many in the current tabpage:

                tabpagewinnr(tabpagenr(), '$')

                List all split windows by tab page:

                let t = 1
                while t <= tabpagenr('$')
                echo 'tab' t . ':'
                let bufs = tabpagebuflist(t)
                let w = 0
                while w < tabpagewinnr(t, '$')
                echo "\t" . bufname(bufs[w])
                let w += 1
                endwhile
                let t += 1
                endwhile
                unlet t w bufs


                Best regards,
                Tony.
                --
                -- Gifts for Men --

                Men are amused by almost any idiot thing -- that is why professional
                ice hockey is so popular -- so buying gifts for them is easy. But you
                should never buy them clothes. Men believe they already have all the
                clothes they will ever need, and new ones make them nervous. For
                example, your average man has 84 ties, but he wears, at most, only
                three of them. He has learned, through humiliating trial and error,
                that if he wears any of the other 81 ties, his wife will probably laugh
                at him ("You're not going to wear THAT tie with that suit, are you?").
                So he has narrowed it down to three safe ties, and has gone several
                years without being laughed at. If you give him a new tie, he will
                pretend to like it, but deep inside he will hate you.

                If you want to give a man something practical, consider tires. More
                than once, I would have gladly traded all the gifts I got for a new set
                of tires.
                -- Dave Barry, "Christmas Shopping: A Survivor's Guide"

                --~--~---------~--~----~------------~-------~--~----~
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
                -~----------~----~----~----~------~----~------~--~---
              • Tim Chase
                ... Ah...something I must have missed somewhere. Other than Try it and see that it happens to be the case , is there some place to learn this in the docs?
                Message 7 of 8 , Sep 1, 2007
                • 0 Attachment
                  > Actually, the windows are identified by sequential numbers from 1 to
                  > winnr('$') and they don't have any fixed ids so they are reliable only
                  > when the window layout has not changed.

                  Ah...something I must have missed somewhere. Other than "Try it
                  and see that it happens to be the case", is there some place to
                  learn this in the docs? The best I could find was

                  "windows are numbered from top-left to bottom-right"

                  in ":help windows.txt". However, this doesn't make it obvious
                  that they're time-sensitive (if I changed my window layout, the
                  buffer<->window mapping changes), and that holes never appear in
                  the count if windows are closed.

                  With that foreknowledge that they're monotonically increasing
                  from 1..winnr('$'), the results are, as Hari and others noted,
                  considerably easier to produce :)

                  -tim




                  --~--~---------~--~----~------------~-------~--~----~
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                  -~----------~----~----~----~------~----~------~--~---
                • Tony Mechelynck
                  ... help winnr() also mentions that winnr( $ ) is both the number of the last window and the window count . IOW, no holes. The fact that the windows are
                  Message 8 of 8 , Sep 1, 2007
                  • 0 Attachment
                    Tim Chase wrote:
                    >> Actually, the windows are identified by sequential numbers from 1 to
                    >> winnr('$') and they don't have any fixed ids so they are reliable only
                    >> when the window layout has not changed.
                    >
                    > Ah...something I must have missed somewhere. Other than "Try it
                    > and see that it happens to be the case", is there some place to
                    > learn this in the docs? The best I could find was
                    >
                    > "windows are numbered from top-left to bottom-right"
                    >
                    > in ":help windows.txt". However, this doesn't make it obvious
                    > that they're time-sensitive (if I changed my window layout, the
                    > buffer<->window mapping changes), and that holes never appear in
                    > the count if windows are closed.
                    >
                    > With that foreknowledge that they're monotonically increasing
                    > from 1..winnr('$'), the results are, as Hari and others noted,
                    > considerably easier to produce :)
                    >
                    > -tim

                    help winnr() also mentions that winnr('$') is both "the number of the last
                    window" and "the window count". IOW, no holes.

                    The fact that the windows are numbered top-to-bottom implies (the way I read
                    it) that when you rearrange your window layout, the window-number-to-buffer
                    mapping changes. Unlike buffer numbers (which are attributed once and for all,
                    and never change even if a lower-numbered buffer is ":bwipeout"ed), a window's
                    number changes whenever a lower-numbered window is opened or closed. (Window
                    number 1 is always at top left and window number winnr('$') is always at
                    bottom right.)


                    Best regards,
                    Tony.
                    --
                    "The whole world is about three drinks behind."
                    -- Humphrey Bogart

                    --~--~---------~--~----~------------~-------~--~----~
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                    -~----------~----~----~----~------~----~------~--~---
                  Your message has been successfully submitted and would be delivered to recipients shortly.