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

Delete all hidden buffers

Expand Messages
  • Ajay Patil
    Hello, Is there a command to delete all the hidden buffers ? I wish to delete only the hidden buffers from the buffer list and not the currently active one.
    Message 1 of 11 , Feb 8, 2005
    View Source
    • 0 Attachment
      Hello,

      Is there a command to delete all the hidden buffers ?

      I wish to delete only the hidden buffers from the buffer list
      and not the currently active one.

      Thanks for the help.
      Ajay
    • David Fishburn
      ... If you are not using some plugin as a buffer manager, I would recommend this one: SelectBuf mappings Author: Hari Krishna Dara
      Message 2 of 11 , Feb 9, 2005
      View Source
      • 0 Attachment
        > -----Original Message-----
        > From: Ajay Patil [mailto:apatil@...]
        > Sent: Tuesday, February 08, 2005 10:49 PM
        > To: vim@...
        > Subject: Delete all hidden buffers
        >
        > Hello,
        >
        > Is there a command to delete all the hidden buffers ?
        >
        > I wish to delete only the hidden buffers from the buffer list
        > and not the currently active one.

        If you are not using some plugin as a buffer manager, I would recommend this
        one:

        " SelectBuf mappings
        " Author: Hari Krishna Dara
        " http://vim.sourceforge.net/script.php?script_id=107

        It allows you to do what you want.

        Dave

        --
        Do you use Vim to edit SQL?
        <Plug>
        Check the description of these plugins:
        dbext.vim - Provides database access to 10 databases (Oracle, Sybase, SQL
        Server, MySQL,...) (http://www.vim.org/scripts/script.php?script_id=356)
        SQLUtilities - Formatting SQL, generate - columns lists, procedures for
        databases (http://www.vim.org/scripts/script.php?script_id=492)
        sql.vim - SQL indent file for Sybase ASA and Oracle
        (http://www.vim.org/scripts/script.php?script_id=495)
        Intellisense for Vim : SQL, C++, CSharp, Java, JSP, XML, HTML
        (http://www.vim.org/scripts/script.php?script_id=747)
        </Plug>
      • Keith Roberts
        ... Here s one way. You could source the following from a file, or wrap them in a function in your vimrc ... ... -Keith
        Message 3 of 11 , Feb 14, 2005
        View Source
        • 0 Attachment
          >-----Original Message-----
          >From: Ajay Patil [mailto:apatil@...]
          >Sent: Tuesday, February 08, 2005 7:49 PM
          >To: vim@...
          >Subject: Delete all hidden buffers
          >
          >Hello,
          >
          >Is there a command to delete all the hidden buffers ?
          >
          >I wish to delete only the hidden buffers from the buffer list
          >and not the currently active one.
          >
          >Thanks for the help.
          >Ajay

          Here's one way. You could source the following from a file, or wrap
          them in a function in your vimrc ...

          :let _bufnr = bufnr() -save current buf#
          :redir > scratch.file -redir output
          :ls -list buffers
          :redir end -end redirection
          :e scratch.file -edit scratch file
          :v/^\s*\d\+\s\+[%#]\=h\s[^+]/ d -del lines w/o hidden unmod. buf
          :%s/^\s*\(\d\+\).*/:bd \1/ -cmds to delete bufs (or :bw)
          :w -save cmds for sourcing
          :source scratch.file -delete the buffers
          :exec 'b' _bufnr -return to orig buffer
          :bw # -wipe scratch buffer
          :!rm scratch.file -del scratch file (!del on Win)

          -Keith
        • Hari Krishna Dara
          ... I prefer doing things without involving temporary files, so here is my take on this: function! DeleteInactiveHiddenBufs() let i = 1 let lastBufNr =
          Message 4 of 11 , Feb 14, 2005
          View Source
          • 0 Attachment
            On Mon, 14 Feb 2005 at 11:25am, Keith Roberts wrote:

            > >-----Original Message-----
            > >From: Ajay Patil [mailto:apatil@...]
            > >Sent: Tuesday, February 08, 2005 7:49 PM
            > >To: vim@...
            > >Subject: Delete all hidden buffers
            > >
            > >Hello,
            > >
            > >Is there a command to delete all the hidden buffers ?
            > >
            > >I wish to delete only the hidden buffers from the buffer list
            > >and not the currently active one.
            > >
            > >Thanks for the help.
            > >Ajay
            >
            > Here's one way. You could source the following from a file, or wrap
            > them in a function in your vimrc ...
            >
            > :let _bufnr = bufnr() -save current buf#
            > :redir > scratch.file -redir output
            > :ls -list buffers
            > :redir end -end redirection
            > :e scratch.file -edit scratch file
            > :v/^\s*\d\+\s\+[%#]\=h\s[^+]/ d -del lines w/o hidden unmod. buf
            > :%s/^\s*\(\d\+\).*/:bd \1/ -cmds to delete bufs (or :bw)
            > :w -save cmds for sourcing
            > :source scratch.file -delete the buffers
            > :exec 'b' _bufnr -return to orig buffer
            > :bw # -wipe scratch buffer
            > :!rm scratch.file -del scratch file (!del on Win)
            >
            > -Keith
            >
            >
            >
            I prefer doing things without involving temporary files, so here is my
            take on this:

            function! DeleteInactiveHiddenBufs()
            let i = 1
            let lastBufNr = bufnr('$')
            let nWipeouts = 0
            while i <= lastBufNr
            if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
            silent exec 'bwipeout' i
            let nWipeouts = nWipeouts + 1
            endif
            let i = i + 1
            endwhile
            echomsg nWipeouts . ' buffer(s) wipedout'
            endfunction
            command! DeleteInactiveHiddenBufs :call DeleteInactiveHiddenBufs()

            --
            HTH,
            Hari



            __________________________________
            Do you Yahoo!?
            Yahoo! Mail - Find what you need with new enhanced search.
            http://info.mail.yahoo.com/mail_250
          • Keith Roberts
            ... I like this, too, but I don t think it does what was asked. ... should be: if bufloaded(i) && bufwinnr(i) == -1 Unless the docs are wrong, bufloaded()==1
            Message 5 of 11 , Feb 14, 2005
            View Source
            • 0 Attachment
              >-----Original Message-----
              >From: Hari Krishna Dara [mailto:hari_vim@...]
              >Sent: Monday, February 14, 2005 3:06 PM
              >To: Keith Roberts
              >Cc: Ajay Patil; vim@...
              >Subject: RE: Delete all hidden buffers
              >
              >
              >On Mon, 14 Feb 2005 at 11:25am, Keith Roberts wrote:
              >
              >> >-----Original Message-----
              >> >From: Ajay Patil [mailto:apatil@...]
              >> >Sent: Tuesday, February 08, 2005 7:49 PM
              >> >To: vim@...
              >> >Subject: Delete all hidden buffers
              >> >
              >> >Hello,
              >> >
              >> >Is there a command to delete all the hidden buffers ?
              >> >
              >> >I wish to delete only the hidden buffers from the buffer
              >list and not
              >> >the currently active one.
              >> >
              >> >Thanks for the help.
              >> >Ajay
              >>
              >> Here's one way. You could source the following from a file, or wrap
              >> them in a function in your vimrc ...
              >>
              >> :let _bufnr = bufnr() -save current buf#
              >> :redir > scratch.file -redir output
              >> :ls -list buffers
              >> :redir end -end redirection
              >> :e scratch.file -edit scratch file
              >> :v/^\s*\d\+\s\+[%#]\=h\s[^+]/ d -del lines w/o hidden unmod. buf
              >> :%s/^\s*\(\d\+\).*/:bd \1/ -cmds to delete bufs (or :bw)
              >> :w -save cmds for sourcing
              >> :source scratch.file -delete the buffers
              >> :exec 'b' _bufnr -return to orig buffer
              >> :bw # -wipe scratch buffer
              >> :!rm scratch.file -del scratch file (!del on Win)
              >>
              >> -Keith
              >>
              >>
              >>
              >I prefer doing things without involving temporary files, so
              >here is my take on this:
              >
              >function! DeleteInactiveHiddenBufs()
              > let i = 1
              > let lastBufNr = bufnr('$')
              > let nWipeouts = 0
              > while i <= lastBufNr
              > if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
              > silent exec 'bwipeout' i
              > let nWipeouts = nWipeouts + 1
              > endif
              > let i = i + 1
              > endwhile
              > echomsg nWipeouts . ' buffer(s) wipedout'
              >endfunction
              >command! DeleteInactiveHiddenBufs :call DeleteInactiveHiddenBufs()
              >
              >--
              >HTH,
              >Hari

              I like this, too, but I don't think it does what was asked.

              > if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1

              should be:

              if bufloaded(i) && bufwinnr(i) == -1

              Unless the docs are wrong, bufloaded()==1 by itself ensures that the
              buffer both exists and is loaded. bufwinnr()==-1 means it's not in a
              window; hence it must be hidden. ! buflisted() would indicate something
              like a help buffer.

              I couldn't think offhand how to distinguish a hidden buffer from all
              others [I forgot about those newer functions], so I did it by brute
              force. :)

              -Keith
            • Keith Roberts
              ... Oops! Modified buffers will break the script, won t they? And we only want to delete unmodified ones, so it should really be: if bufloaded(i) &&
              Message 6 of 11 , Feb 14, 2005
              View Source
              • 0 Attachment
                > -----Original Message-----
                > From: Keith Roberts [mailto:kroberts@...]
                > Sent: Monday, February 14, 2005 3:27 PM
                > To: Hari Krishna Dara
                > Cc: Ajay Patil; vim@...
                > Subject: RE: Delete all hidden buffers
                >
                Hari Krishna Dara wrote:
                >> On Mon, 14 Feb 2005 at 11:25am, Keith Roberts wrote:
                >>
                >>>> -----Original Message-----
                >>>> From: Ajay Patil [mailto:apatil@...]
                >>>> Sent: Tuesday, February 08, 2005 7:49 PM
                >>>> To: vim@...
                >>>> Subject: Delete all hidden buffers
                >>>>
                >>>> Hello,
                >>>>
                >>>> Is there a command to delete all the hidden buffers ?
                >>>>
                >>>> I wish to delete only the hidden buffers from the buffer list and
                >>>> not the currently active one.
                >>>>
                >>>> Thanks for the help.
                >>>> Ajay
                >>>
                >>> Here's one way. You could source the following from a file, or wrap
                >>> them in a function in your vimrc ...
                >>>
                >>>> let _bufnr = bufnr() -save current buf#
                >>>> redir > scratch.file -redir output
                >>>> ls -list buffers
                >>>> redir end -end redirection
                >>>> e scratch.file -edit scratch file
                >>>> v/^\s*\d\+\s\+[%#]\=h\s[^+]/ d -del lines w/o hidden unmod. buf
                >>> :%s/^\s*\(\d\+\).*/:bd \1/ -cmds to delete bufs (or :bw)
                >>>> w -save cmds for sourcing
                >>>> source scratch.file -delete the buffers
                >>>> exec 'b' _bufnr -return to orig buffer
                >>>> bw # -wipe scratch buffer
                >>> :!rm scratch.file -del scratch file (!del on Win)
                >>>
                >>> -Keith
                >>>
                >>>
                >>>
                >> I prefer doing things without involving temporary files, so here is
                >> my take on this:
                >>
                >> function! DeleteInactiveHiddenBufs()
                >> let i = 1
                >> let lastBufNr = bufnr('$')
                >> let nWipeouts = 0
                >> while i <= lastBufNr
                >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                >> silent exec 'bwipeout' i let nWipeouts = nWipeouts + 1
                >> endif
                >> let i = i + 1
                >> endwhile
                >> echomsg nWipeouts . ' buffer(s) wipedout'
                >> endfunction
                >> command! DeleteInactiveHiddenBufs :call DeleteInactiveHiddenBufs()
                >>
                >> --
                >> HTH,
                >> Hari
                >
                > I like this, too, but I don't think it does what was asked.
                >
                >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                >
                > should be:
                >
                > if bufloaded(i) && bufwinnr(i) == -1
                >
                > Unless the docs are wrong, bufloaded()==1 by itself ensures
                > that the buffer both exists and is loaded. bufwinnr()==-1
                > means it's not in a window; hence it must be hidden. !
                > buflisted() would indicate something like a help buffer.
                >
                > I couldn't think offhand how to distinguish a hidden buffer
                > from all others [I forgot about those newer functions], so I did it
                > by brute force. :)
                >
                > -Keith

                Oops! Modified buffers will break the script, won't they? And we only
                want to delete unmodified ones, so it should really be:

                if bufloaded(i) && bufwinnr(i) == -1 && ! getbufvar(i,"&mod")

                -Keith
              • Ajay Patil
                Thank you for the useful script. Ajay ... email-body was scanned and no virus found
                Message 7 of 11 , Feb 14, 2005
                View Source
                • 0 Attachment
                  Thank you for the useful script.

                  Ajay

                  > >
                  > Hari Krishna Dara wrote:
                  > >> On Mon, 14 Feb 2005 at 11:25am, Keith Roberts wrote:
                  > >>
                  > >>>> -----Original Message-----
                  > >>>> From: Ajay Patil [mailto:apatil@...]
                  > >>>> Sent: Tuesday, February 08, 2005 7:49 PM
                  > >>>> To: vim@...
                  > >>>> Subject: Delete all hidden buffers
                  > >>>>
                  > >>>> Hello,
                  > >>>>
                  > >>>> Is there a command to delete all the hidden buffers ?
                  > >>>>
                  > >>>> I wish to delete only the hidden buffers from the buffer list and
                  > >>>> not the currently active one.
                  > >>>>
                  > >>>> Thanks for the help.
                  > >>>> Ajay
                  > >>>
                  > >>> Here's one way. You could source the following from a file,
                  > or wrap
                  > >>> them in a function in your vimrc ...
                  > >>>
                  > >>>> let _bufnr = bufnr() -save current buf#
                  > >>>> redir > scratch.file -redir output
                  > >>>> ls -list buffers
                  > >>>> redir end -end redirection
                  > >>>> e scratch.file -edit scratch file
                  > >>>> v/^\s*\d\+\s\+[%#]\=h\s[^+]/ d -del lines w/o hidden
                  > unmod. buf
                  > >>> :%s/^\s*\(\d\+\).*/:bd \1/ -cmds to delete bufs (or :bw)
                  > >>>> w -save cmds for sourcing
                  > >>>> source scratch.file -delete the buffers
                  > >>>> exec 'b' _bufnr -return to orig buffer
                  > >>>> bw # -wipe scratch buffer
                  > >>> :!rm scratch.file -del scratch file (!del on
                  > Win)>>>
                  > >>> -Keith
                  > >>>
                  > >>>
                  > >>>
                  > >> I prefer doing things without involving temporary files, so
                  > here is
                  > >> my take on this:
                  > >>
                  > >> function! DeleteInactiveHiddenBufs()
                  > >> let i = 1
                  > >> let lastBufNr = bufnr('$')
                  > >> let nWipeouts = 0
                  > >> while i <= lastBufNr
                  > >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                  > >> silent exec 'bwipeout' i let nWipeouts = nWipeouts + 1
                  > >> endif
                  > >> let i = i + 1
                  > >> endwhile
                  > >> echomsg nWipeouts . ' buffer(s) wipedout'
                  > >> endfunction
                  > >> command! DeleteInactiveHiddenBufs :call DeleteInactiveHiddenBufs()
                  > >>
                  > >> --
                  > >> HTH,
                  > >> Hari
                  > >
                  > > I like this, too, but I don't think it does what was asked.
                  > >
                  > >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                  > >
                  > > should be:
                  > >
                  > > if bufloaded(i) && bufwinnr(i) == -1
                  > >
                  > > Unless the docs are wrong, bufloaded()==1 by itself ensures
                  > > that the buffer both exists and is loaded. bufwinnr()==-1
                  > > means it's not in a window; hence it must be hidden. !
                  > > buflisted() would indicate something like a help buffer.
                  > >
                  > > I couldn't think offhand how to distinguish a hidden buffer
                  > > from all others [I forgot about those newer functions], so I did it
                  > > by brute force. :)
                  > >
                  > > -Keith
                  >
                  > Oops! Modified buffers will break the script, won't they? And we
                  > onlywant to delete unmodified ones, so it should really be:
                  >
                  > if bufloaded(i) && bufwinnr(i) == -1 && ! getbufvar(i,"&mod")
                  >
                  > -Keith
                  >
                • Hari Krishna Dara
                  ... Yes, I forgot about modified status, thanks for correcting it. Thankfully the :bw without the bang would have failed on a modified buffer, so though you
                  Message 8 of 11 , Feb 15, 2005
                  View Source
                  • 0 Attachment
                    On Mon, 14 Feb 2005 at 3:39pm, Keith Roberts wrote:

                    > > -----Original Message-----
                    > > From: Keith Roberts [mailto:kroberts@...]
                    > > Sent: Monday, February 14, 2005 3:27 PM
                    > > To: Hari Krishna Dara
                    > > Cc: Ajay Patil; vim@...
                    > > Subject: RE: Delete all hidden buffers
                    > >
                    > Hari Krishna Dara wrote:
                    > >> On Mon, 14 Feb 2005 at 11:25am, Keith Roberts wrote:
                    > >>
                    > >>>> -----Original Message-----
                    > >>>> From: Ajay Patil [mailto:apatil@...]
                    > >>>> Sent: Tuesday, February 08, 2005 7:49 PM
                    > >>>> To: vim@...
                    > >>>> Subject: Delete all hidden buffers
                    > >>>>
                    > >>>> Hello,
                    > >>>>
                    > >>>> Is there a command to delete all the hidden buffers ?
                    > >>>>
                    > >>>> I wish to delete only the hidden buffers from the buffer list and
                    > >>>> not the currently active one.
                    > >>>>
                    > >>>> Thanks for the help.
                    > >>>> Ajay
                    > >>>
                    > >>> Here's one way. You could source the following from a file, or wrap
                    > >>> them in a function in your vimrc ...
                    > >>>
                    > >>>> let _bufnr = bufnr() -save current buf#
                    > >>>> redir > scratch.file -redir output
                    > >>>> ls -list buffers
                    > >>>> redir end -end redirection
                    > >>>> e scratch.file -edit scratch file
                    > >>>> v/^\s*\d\+\s\+[%#]\=h\s[^+]/ d -del lines w/o hidden unmod. buf
                    > >>> :%s/^\s*\(\d\+\).*/:bd \1/ -cmds to delete bufs (or :bw)
                    > >>>> w -save cmds for sourcing
                    > >>>> source scratch.file -delete the buffers
                    > >>>> exec 'b' _bufnr -return to orig buffer
                    > >>>> bw # -wipe scratch buffer
                    > >>> :!rm scratch.file -del scratch file (!del on Win)
                    > >>>
                    > >>> -Keith
                    > >>>
                    > >>>
                    > >>>
                    > >> I prefer doing things without involving temporary files, so here is
                    > >> my take on this:
                    > >>
                    > >> function! DeleteInactiveHiddenBufs()
                    > >> let i = 1
                    > >> let lastBufNr = bufnr('$')
                    > >> let nWipeouts = 0
                    > >> while i <= lastBufNr
                    > >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                    > >> silent exec 'bwipeout' i let nWipeouts = nWipeouts + 1
                    > >> endif
                    > >> let i = i + 1
                    > >> endwhile
                    > >> echomsg nWipeouts . ' buffer(s) wipedout'
                    > >> endfunction
                    > >> command! DeleteInactiveHiddenBufs :call DeleteInactiveHiddenBufs()
                    > >>
                    > >> --
                    > >> HTH,
                    > >> Hari
                    > >
                    > > I like this, too, but I don't think it does what was asked.
                    > >
                    > >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                    > >
                    > > should be:
                    > >
                    > > if bufloaded(i) && bufwinnr(i) == -1
                    > >
                    > > Unless the docs are wrong, bufloaded()==1 by itself ensures
                    > > that the buffer both exists and is loaded. bufwinnr()==-1
                    > > means it's not in a window; hence it must be hidden. !
                    > > buflisted() would indicate something like a help buffer.
                    > >
                    > > I couldn't think offhand how to distinguish a hidden buffer
                    > > from all others [I forgot about those newer functions], so I did it
                    > > by brute force. :)
                    > >
                    > > -Keith
                    >
                    > Oops! Modified buffers will break the script, won't they? And we only
                    > want to delete unmodified ones, so it should really be:
                    >
                    > if bufloaded(i) && bufwinnr(i) == -1 && ! getbufvar(i,"&mod")
                    >
                    > -Keith
                    >
                    >
                    >

                    Yes, I forgot about modified status, thanks for correcting it.
                    Thankfully the :bw without the bang would have failed on a modified
                    buffer, so though you would see a bunch of errors, it wouldn't have done
                    any harm.

                    About my original condition, bufexists() was used to make sure we are
                    testing on a buffer which actually exists (all the buffer numbers in a
                    range are not necessarily valid). This is just a good programming
                    habbit, and is not really required. The buflisted() checks the
                    'buflisted' setting for the buffer, and this is what is set for a buffer
                    that is not listed (ie, buffers not shown by the :ls command, but only
                    shown using :ls! command). This is what I thought the OP wanted to do,
                    but now when I went back and read the original post, it just says
                    "delete all the hidden buffers". Now I am not sure what the OP meant by
                    hidden buffers. Hidden buffers in the Vim lingo are buffers that are
                    loaded but not shown (either because they have 'bufhidden' option set,
                    or the global 'hidden' option is set). Why would anyone want to delete
                    these buffers? This usually means deleting anything that is not
                    currently active, which didn't make sense. If this is what the OP
                    wanted, then the question I would expect to see is "delete all the
                    buffers that are not currently shown". Provided this is the question,
                    your alternative wouldn't work either. The bufloaded() would be true
                    even for the active buffers, so the command would wipeout any buffer
                    that is not modified (resulting in closing some windows). Correct me if
                    I am wrong.

                    The correct condition is pending on further clarification of the OP.

                    --
                    HTH,
                    Hari



                    __________________________________
                    Do you Yahoo!?
                    The all-new My Yahoo! - What will yours do?
                    http://my.yahoo.com
                  • Ajay Patil
                    Hello, ... Just wanted to clarify my question. I wish to delete all the buffers that are not currently shown. Thanks for helping out. Ajay ... 差出人:
                    Message 9 of 11 , Feb 15, 2005
                    View Source
                    • 0 Attachment
                      Hello,

                      > The correct condition is pending on further clarification of the OP.

                      Just wanted to clarify my question.
                      I wish to delete all the buffers that are not currently shown.

                      Thanks for helping out.
                      Ajay

                      ----- Original Message -----
                      : Hari Krishna Dara <hari_vim@...>
                      : 2005216 () 4:22
                      : RE: Delete all hidden buffers

                      >
                      > On Mon, 14 Feb 2005 at 3:39pm, Keith Roberts wrote:
                      >
                      > > > -----Original Message-----
                      > > > From: Keith Roberts [mailto:kroberts@...]
                      > > > Sent: Monday, February 14, 2005 3:27 PM
                      > > > To: Hari Krishna Dara
                      > > > Cc: Ajay Patil; vim@...
                      > > > Subject: RE: Delete all hidden buffers
                      > > >
                      > >>
                      > > >>>> -----Original Message-----
                      > > >>>> From: Ajay Patil [mailto:apatil@...]
                      > > >>>> Sent: Tuesday, February 08, 2005 7:49 PM
                      > > >>>> To: vim@...
                      > > >>>> Subject: Delete all hidden buffers
                      > > >>>>
                      > > >>>> Hello,
                      > > >>>>
                      > > >>>> Is there a command to delete all the hidden buffers ?
                      > > >>>>
                      > > >>>> I wish to delete only the hidden buffers from the buffer
                      > list and
                      > > >>>> not the currently active one.
                      > > >>>>
                      > > >>>> Thanks for the help.
                      > > >>>> Ajay
                      > > >>>
                      > > >>> Here's one way. You could source the following from a file,
                      > or wrap
                      > > >>> them in a function in your vimrc ...
                      > > >>>
                      > > >>>> let _bufnr = bufnr() -save current buf#
                      > > >>>> redir > scratch.file -redir output
                      > > >>>> ls -list buffers
                      > > >>>> redir end -end redirection
                      > > >>>> e scratch.file -edit scratch file
                      > > >>>> v/^\s*\d\+\s\+[%#]\=h\s[^+]/ d -del lines w/o hidden
                      > unmod. buf
                      > > >>> :%s/^\s*\(\d\+\).*/:bd \1/ -cmds to delete bufs (or
                      > :bw)> >>>> w -save cmds for sourcing
                      > > >>>> source scratch.file -delete the buffers
                      > > >>>> exec 'b' _bufnr -return to orig buffer
                      > > >>>> bw # -wipe scratch buffer
                      > > >>> :!rm scratch.file -del scratch file (!del
                      > on Win)
                      > > >>>
                      > > >>> -Keith
                      > > >>>
                      > > >>>
                      > > >>>
                      > > >> I prefer doing things without involving temporary files, so
                      > here is
                      > > >> my take on this:
                      > > >>
                      > > >> function! DeleteInactiveHiddenBufs()
                      > > >> let i = 1
                      > > >> let lastBufNr = bufnr('$')
                      > > >> let nWipeouts = 0
                      > > >> while i <= lastBufNr
                      > > >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                      > > >> silent exec 'bwipeout' i let nWipeouts = nWipeouts + 1
                      > > >> endif
                      > > >> let i = i + 1
                      > > >> endwhile
                      > > >> echomsg nWipeouts . ' buffer(s) wipedout'
                      > > >> endfunction
                      > > >> command! DeleteInactiveHiddenBufs :call
                      > DeleteInactiveHiddenBufs()> >>
                      > > >> --
                      > > >> HTH,
                      > > >> Hari
                      > > >
                      > > > I like this, too, but I don't think it does what was asked.
                      > > >
                      > > >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                      > > >
                      > > > should be:
                      > > >
                      > > > if bufloaded(i) && bufwinnr(i) == -1
                      > > >
                      > > > Unless the docs are wrong, bufloaded()==1 by itself ensures
                      > > > that the buffer both exists and is loaded. bufwinnr()==-1
                      > > > means it's not in a window; hence it must be hidden. !
                      > > > buflisted() would indicate something like a help buffer.
                      > > >
                      > > > I couldn't think offhand how to distinguish a hidden buffer
                      > > > from all others [I forgot about those newer functions], so I
                      > did it
                      > > > by brute force. :)
                      > > >
                      > > > -Keith
                      > >
                      > > Oops! Modified buffers will break the script, won't they? And
                      > we only
                      > > want to delete unmodified ones, so it should really be:
                      > >
                      > > if bufloaded(i) && bufwinnr(i) == -1 && ! getbufvar(i,"&mod")
                      > >
                      > > -Keith
                      > >
                      > >
                      > >
                      >
                      > Yes, I forgot about modified status, thanks for correcting it.
                      > Thankfully the :bw without the bang would have failed on a modified
                      > buffer, so though you would see a bunch of errors, it wouldn't
                      > have done
                      > any harm.
                      >
                      > About my original condition, bufexists() was used to make sure we are
                      > testing on a buffer which actually exists (all the buffer numbers
                      > in a
                      > range are not necessarily valid). This is just a good programming
                      > habbit, and is not really required. The buflisted() checks the
                      > 'buflisted' setting for the buffer, and this is what is set for a
                      > bufferthat is not listed (ie, buffers not shown by the :ls
                      > command, but only
                      > shown using :ls! command). This is what I thought the OP wanted to do,
                      > but now when I went back and read the original post, it just says
                      > "delete all the hidden buffers". Now I am not sure what the OP
                      > meant by
                      > hidden buffers. Hidden buffers in the Vim lingo are buffers that are
                      > loaded but not shown (either because they have 'bufhidden' option set,
                      > or the global 'hidden' option is set). Why would anyone want to delete
                      > these buffers? This usually means deleting anything that is not
                      > currently active, which didn't make sense. If this is what the OP
                      > wanted, then the question I would expect to see is "delete all the
                      > buffers that are not currently shown". Provided this is the question,
                      > your alternative wouldn't work either. The bufloaded() would be true
                      > even for the active buffers, so the command would wipeout any buffer
                      > that is not modified (resulting in closing some windows). Correct
                      > me if
                      > I am wrong.
                      >
                      > The correct condition is pending on further clarification of the OP.
                      >
                      > --
                      > HTH,
                      > Hari
                    • Keith Roberts
                      ... I haven t tested it, and I know you have worlds more experience than I at scripting, but I relied on the docs, which imply that checking for bufexists()
                      Message 10 of 11 , Feb 16, 2005
                      View Source
                      • 0 Attachment
                        >-----Original Message-----
                        >From: Hari Krishna Dara [mailto:hari_vim@...]
                        >Sent: Tuesday, February 15, 2005 11:22 AM
                        >To: Keith Roberts
                        >Cc: Ajay Patil; vim@...
                        >Subject: RE: Delete all hidden buffers
                        >
                        >
                        >On Mon, 14 Feb 2005 at 3:39pm, Keith Roberts wrote:
                        >
                        >> > -----Original Message-----
                        >> > From: Keith Roberts [mailto:kroberts@...]
                        >> > Sent: Monday, February 14, 2005 3:27 PM
                        >> > To: Hari Krishna Dara
                        >> > Cc: Ajay Patil; vim@...
                        >> > Subject: RE: Delete all hidden buffers
                        >> >
                        >> Hari Krishna Dara wrote:
                        >> >> On Mon, 14 Feb 2005 at 11:25am, Keith Roberts wrote:
                        >> >>
                        >> >>>> -----Original Message-----
                        >> >>>> From: Ajay Patil [mailto:apatil@...]
                        >> >>>> Sent: Tuesday, February 08, 2005 7:49 PM
                        >> >>>> To: vim@...
                        >> >>>> Subject: Delete all hidden buffers
                        >> >>>>
                        >> >>>> Hello,
                        >> >>>>
                        >> >>>> Is there a command to delete all the hidden buffers ?
                        >> >>>>
                        >> >>>> I wish to delete only the hidden buffers from the
                        >> >>>> buffer list and
                        >> >>>> not the currently active one.
                        >> >>>>
                        >> >>>> Thanks for the help.
                        >> >>>> Ajay
                        >> >>>
                        >> >>> Here's one way. [snip]
                        >> >>>
                        >> >>> -Keith
                        >> >>>
                        >> >> I prefer doing things without involving temporary files,
                        >> >> so here is
                        >> >> my take on this:
                        >> >> [snip]
                        >> >> --
                        >> >> HTH,
                        >> >> Hari
                        >> >
                        >> > I like this, too, but I don't think it does what was asked.
                        >> >
                        >> >> if bufexists(i) && ! buflisted(i) && bufwinnr(i) == -1
                        >> >
                        >> > should be:
                        >> >
                        >> > if bufloaded(i) && bufwinnr(i) == -1
                        >> >
                        >> > Unless the docs are wrong, bufloaded()==1 by itself
                        >> > ensures that the
                        >> > buffer both exists and is loaded. bufwinnr()==-1 means
                        >> > it's not in
                        >> > a window; hence it must be hidden. !
                        >> > buflisted() would indicate something like a help buffer.
                        >> >
                        >> > I couldn't think offhand how to distinguish a hidden
                        >> > buffer from all
                        >> > others [I forgot about those newer functions], so I did it
                        >> > by brute force. :)
                        >> >
                        >> > -Keith
                        >>
                        >> Oops! Modified buffers will break the script, won't they? And we
                        >> only want to delete unmodified ones, so it should really be:
                        >>
                        >> if bufloaded(i) && bufwinnr(i) == -1 && !
                        >> \ getbufvar(i,"&mod")
                        >>
                        >> -Keith
                        >>
                        >>
                        >>
                        >
                        >Yes, I forgot about modified status, thanks for correcting it.
                        >Thankfully the :bw without the bang would have failed on a
                        >modified buffer, so though you would see a bunch of errors, it
                        >wouldn't have done any harm.
                        >
                        >About my original condition, bufexists() was used to make sure
                        >we are testing on a buffer which actually exists (all the
                        >buffer numbers in a range are not necessarily valid). This is
                        >just a good programming habbit, and is not really required.
                        >The buflisted() checks the 'buflisted' setting for the buffer,
                        >and this is what is set for a buffer that is not listed (ie,
                        >buffers not shown by the :ls command, but only shown using
                        >:ls! command).

                        I haven't tested it, and I know you have worlds more experience than I
                        at scripting, but I relied on the docs, which imply that checking for
                        bufexists() before using either buflisted() or bufloaded() is
                        unnecessary. :)

                        >This is what I thought the OP wanted to do, but
                        >now when I went back and read the original post, it just says
                        >"delete all the hidden buffers".

                        <tongue in cheek>
                        The OP says, "delete all the hidden buffers FROM THE BUFFER LIST", which
                        I think is explicit enough. If the OP meant buffers listed by :ls! but
                        not :ls, then they would have known enough to use the term "unlisted"
                        rather than "hidden" [since knowing to use :ls! to see them implies
                        knowledge of the difference between the two commands, which means
                        they've read the help on them, which means they've seen the definitions
                        of "hidden" and "unlisted"].
                        <\tongue in cheek>

                        Moreover, the word/status "hidden" is reinforced every time you do :ls
                        because you see the "h" flag. The others you speak of aren't really
                        "hidden"; they are "unloaded" [hence the lack of any flag at all]. At
                        least, that's the way I read it. :)

                        >Now I am not sure what the OP
                        >meant by hidden buffers. Hidden buffers in the Vim lingo are
                        >buffers that are loaded but not shown (either because they
                        >have 'bufhidden' option set, or the global 'hidden' option is
                        >set). Why would anyone want to delete these buffers? This
                        >usually means deleting anything that is not currently active,
                        >which didn't make sense. If this is what the OP wanted, then
                        >the question I would expect to see is "delete all the buffers
                        >that are not currently shown". Provided this is the question,
                        >your alternative wouldn't work either. The bufloaded() would
                        >be true even for the active buffers, so the command would
                        >wipeout any buffer that is not modified (resulting in closing
                        >some windows). Correct me if I am wrong.

                        That's what the check for winnr() == -1 is for in my command, to ensure
                        that the buffer is *not* in a window, and therefore is not "active". So
                        what we end up with is:

                        if bufloaded(i) -is "shown in a window or hidden"
                        && bufwinnr(i) == -1 -is not in a window (thus hidden)
                        && ! getbufvar(i,"&mod") -is not modified

                        -Keith
                      • Hari Krishna Dara
                        On Wed, 16 Feb 2005 at 8:59am, Keith Roberts wrote: [snip] ... Yes, it should be unnecessary, but as I said, it is just the force of habbit (in this case, I
                        Message 11 of 11 , Feb 16, 2005
                        View Source
                        • 0 Attachment
                          On Wed, 16 Feb 2005 at 8:59am, Keith Roberts wrote:

                          [snip]

                          > at scripting, but I relied on the docs, which imply that checking for
                          > bufexists() before using either buflisted() or bufloaded() is
                          > unnecessary. :)

                          Yes, it should be unnecessary, but as I said, it is just the force of
                          habbit (in this case, I just feel more comfortable if I don't call a
                          method with invalid argument, in this case an invalid buffer number).

                          [snip]

                          >
                          > That's what the check for winnr() == -1 is for in my command, to ensure
                          > that the buffer is *not* in a window, and therefore is not "active". So
                          > what we end up with is:
                          >
                          > if bufloaded(i) -is "shown in a window or hidden"
                          > && bufwinnr(i) == -1 -is not in a window (thus hidden)
                          > && ! getbufvar(i,"&mod") -is not modified
                          >
                          > -Keith
                          >

                          Sorry, that was my mistake, I failed to see that you had a check on
                          bufwinnr(). As per OP clarification in another email, his intention was
                          in deed to remove all the buffers that are not currently shown, so your
                          condition is perfectly right for him.

                          --
                          Thanks again,
                          Hari



                          __________________________________
                          Do you Yahoo!?
                          Yahoo! Mail - Find what you need with new enhanced search.
                          http://info.mail.yahoo.com/mail_250
                        Your message has been successfully submitted and would be delivered to recipients shortly.