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

Grep in loaded buffers

Expand Messages
  • Ralph Henderson
    Sorry if this is obvious or documented. What is the easiest way to grep in all the files in the buffer list? i.e. I would like to do a find (/) but in all
    Message 1 of 8 , Nov 1, 2004
      Sorry if this is obvious or documented.

      What is the easiest way to grep in all the files in the buffer list?
      i.e. I would like to do a find (/) but in all buffers, not just the
      current one.

      Many thanks.

      Ralph Henderson
    • Tim Chase
      ... I don t think there s an easy way to get the normal find (/) to continue to the next buffer. However, you can perform any Ex command across all buffers
      Message 2 of 8 , Nov 1, 2004
        > What is the easiest way to grep in all the files in the buffer list?
        > i.e. I would like to do a find (/) but in all buffers, not just the
        > current one.

        I don't think there's an easy way to get the normal "find" (/) to
        continue to the next buffer. However, you can perform any Ex
        command across all buffers using the "bufdo" command. Thus, you
        could do something like

        :bufdo g/regexp/#

        which would return the line-numbers of the lines on which
        "regexp" is found in all buffers. It has cousins "argdo" and
        "windo" which work similarly for arguments and windows. Things
        other than "#" can be used if needed...if you needed to delete
        all lines containing "regexp", you could change the "#" to a "d".
        Note that for items that change the buffer you won't be able to
        leave the window unless you've either saved it (appending "|w" to
        the command) or by setting 'hidden' (my preferred method, as it
        allows you to review changes before doing a ":wall")

        For more info, see
        :help bufdo
        :he argdo
        :he windo

        -tim
      • Keith Roberts
        ... [This is untested; I don t know whether it will write to the current @a or @a for each buffer.] Or, use :redir! {filename} ... Make sure {filename} isn t
        Message 3 of 8 , Nov 1, 2004
          >-----Original Message-----
          >From: Ralph Henderson [mailto:ralph.henderson@...]
          >Sent: Monday, November 01, 2004 10:05 AM
          >To: vim@...
          >Subject: Grep in loaded buffers
          >
          >Sorry if this is obvious or documented.
          >
          >What is the easiest way to grep in all the files in the buffer list?
          >i.e. I would like to do a find (/) but in all buffers, not
          >just the current one.
          >
          >Many thanks.
          >
          >Ralph Henderson

          How about something like:

          :let @a = ''
          :redir @a
          :bufdo s/text/text/
          :redir end

          [This is untested; I don't know whether it will write to the current @a
          or @a for each buffer.]

          Or, use :redir! {filename} ... Make sure {filename} isn't in the buffer
          list, or it'll double the output when it gets to it, I assume. See :h
          :redir; you would probably want to put it in a function and use :silent
          call Function() so it doesn't all come to the screen as well.

          -Keith
        • Antoine J. Mechelynck
          Keith Roberts wrote: [...] ... AFAIK, there is no different @a for each buffer. ... Best regards, Tony.
          Message 4 of 8 , Nov 1, 2004
            Keith Roberts <kroberts@...> wrote:
            [...]
            > How about something like:
            >
            > :let @a = ''
            > :redir @a
            > :bufdo s/text/text/
            > :redir end
            >
            > [This is untested; I don't know whether it will write to the current
            > @a or @a for each buffer.]

            AFAIK, there is no different @a for each buffer.

            >
            > Or, use :redir! {filename} ... Make sure {filename} isn't in the
            > buffer list, or it'll double the output when it gets to it, I assume.
            > See :h :redir; you would probably want to put it in a function and use
            > :silent call Function() so it doesn't all come to the screen as well.
            >
            > -Keith

            Best regards,
            Tony.
          • Hari Krishna Dara
            ... This solution works well and is exactly what my greputils.vim does when executing the :BufGrep command. In addition it provides the results in a quickfix
            Message 5 of 8 , Nov 1, 2004
              On Mon, 1 Nov 2004 at 10:17am, Keith Roberts wrote:

              > >-----Original Message-----
              > >From: Ralph Henderson [mailto:ralph.henderson@...]
              > >Sent: Monday, November 01, 2004 10:05 AM
              > >To: vim@...
              > >Subject: Grep in loaded buffers
              > >
              > >Sorry if this is obvious or documented.
              > >
              > >What is the easiest way to grep in all the files in the buffer list?
              > >i.e. I would like to do a find (/) but in all buffers, not
              > >just the current one.
              > >
              > >Many thanks.
              > >
              > >Ralph Henderson
              >
              > How about something like:
              >
              > :let @a = ''
              > :redir @a
              > :bufdo s/text/text/
              > :redir end
              >
              > [This is untested; I don't know whether it will write to the current @a
              > or @a for each buffer.]
              >
              > Or, use :redir! {filename} ... Make sure {filename} isn't in the buffer
              > list, or it'll double the output when it gets to it, I assume. See :h
              > :redir; you would probably want to put it in a function and use :silent
              > call Function() so it doesn't all come to the screen as well.
              >
              > -Keith
              >

              This solution works well and is exactly what my greputils.vim does when
              executing the :BufGrep command. In addition it provides the results in a
              quickfix like window.

              http://www.vim.org/scripts/script.php?script_id=1062

              There are also :WinGrep and :ArgGrep on the lines of :windo and :argdo
              and in addition a :GrepBufs command to do the same for given arbitrary
              list of buffers.

              --
              HTH,
              Hari



              __________________________________
              Do you Yahoo!?
              Check out the new Yahoo! Front Page.
              www.yahoo.com
            • Ralph Henderson
              Thanks to all! ... worked well; I ll also look up those grep utils, hari. -Ralph
              Message 6 of 8 , Nov 1, 2004
                Thanks to all!

                :bufdo g/regexp/#

                worked well; I'll also look up those grep utils, hari.

                -Ralph


                >> What is the easiest way to grep in all the files in the buffer list?
                >> i.e. I would like to do a find (/) but in all buffers, not just the
                >> current one.
                >
                >I don't think there's an easy way to get the normal "find" (/) to
                >continue to the next buffer. However, you can perform any Ex
                >command across all buffers using the "bufdo" command. Thus, you
                >could do something like
                >
                > :bufdo g/regexp/#
                >
                >which would return the line-numbers of the lines on which
                >"regexp" is found in all buffers. It has cousins "argdo" and
                >"windo" which work similarly for arguments and windows. Things
                >other than "#" can be used if needed...if you needed to delete
                >all lines containing "regexp", you could change the "#" to a "d".
                > Note that for items that change the buffer you won't be able to
                >leave the window unless you've either saved it (appending "|w" to
                >the command) or by setting 'hidden' (my preferred method, as it
                >allows you to review changes before doing a ":wall")
                >
                >For more info, see
                > :help bufdo
                > :he argdo
                > :he windo
                >
                >-tim
                >
                >
                >
                >
                >
                >
                >
                >
                >
              • Keith Roberts
                ... Yeah. Well. That s what I get for answering a question with half my brain thinking about something else. :) Maybe I had a in mind. :)) -Keith
                Message 7 of 8 , Nov 1, 2004
                  >-----Original Message-----
                  >From: Antoine J. Mechelynck [mailto:antoine.mechelynck@...]
                  >Sent: Monday, November 01, 2004 10:36 AM
                  >To: Keith Roberts; ralph.henderson@...; vim@...
                  >Subject: Re: Grep in loaded buffers
                  >
                  >Keith Roberts <kroberts@...> wrote:
                  >[...]
                  >> How about something like:
                  >>
                  >> :let @a = ''
                  >> :redir @a
                  >> :bufdo s/text/text/
                  >> :redir end
                  >>
                  >> [This is untested; I don't know whether it will write to the current
                  >> @a or @a for each buffer.]
                  >
                  >AFAIK, there is no different @a for each buffer.
                  >
                  >Best regards,
                  >Tony.

                  Yeah. Well. That's what I get for answering a question with half my
                  brain thinking about something else. :) Maybe I had 'a in mind. :))

                  -Keith
                • Dave Silvia
                  easiest is somewhat subjective. If you want something easy to remember and which has ease of flexibility, I would suggest: VimgrepBufs
                  Message 8 of 8 , Nov 1, 2004
                    'easiest' is somewhat subjective. If you want something 'easy' to remember
                    and which has 'ease' of flexibility, I would suggest:

                    VimgrepBufs <regexp> [options]
                    VimgrepBufsToBuf <regexp> [options]

                    The first outputs the search info to the command line, like :g/regexp....
                    However, unlike :g/..., it gives info on pattern not found, etc. The second
                    outputs the search info to a named buffer, a bit more complicated to do with
                    :g/... .

                    Synopsis:
                    command VimgrepBufs srchpat[ -M][ -t]
                    command VimgrepBufsToBuf srchpat[ -M][ -t]
                    Where: -M == 'match case' and -t == 'terse', i.e., no 'not found' type info.

                    See:

                    http://www.vim.org/account/profile.php?user_id=5397 vimgrep.vim vsutil.vim

                    HTH:

                    MfG,
                    Dave S.

                    -----Original Message-----
                    From: Ralph Henderson [mailto:ralph.henderson@...]
                    Sent: Monday, November 01, 2004 12:05 PM
                    To: vim@...
                    Subject: Grep in loaded buffers

                    Sorry if this is obvious or documented.

                    What is the easiest way to grep in all the files in the buffer list?
                    i.e. I would like to do a find (/) but in all buffers, not just the
                    current one.

                    Many thanks.

                    Ralph Henderson
                  Your message has been successfully submitted and would be delivered to recipients shortly.