Thanks.

On Fri, 8 Dec 2006, A.J.Mechelynck wrote:

Ben K. wrote:

Hi,

Is there a way to do grep only within the open files (buffers)?
I did :he grep and bufdo but it didn't seem to give me the option.

Thanks.



Ben K.
Developer
http://benix.tamu.edu


All the following is for Vim version 7 ONLY.

To grep in all files named on the vim command-line, assuming no spaces in their names:

        :exe 'vimgrep /pattern/g' join(argv(), ' ')

see
        :help :vimgrep
        :help join()
        :help argv()

To grep in all loaded buffers it's (IIUC) more complicated:

        let buflist = []
        let bufnr = 0
        while bufnr < bufnr("$")
                let bufnr += 1
                if buflisted(bufnr)
                        let buflist += [ bufname(bufnr) ]
                endif
        endwhile
        exe 'vimgrep /pattern/g' join(buflist, ' ')

Replace buflisted() by bufexists() to search also unlisted buffers. How to do the same if filenames can include spaces is left as an exercise to the user (it will be different for Unix shells or Dos shells).


Best regards,
Tony.


Regards,

Ben K.
Developer
http://benix.tamu.edu

Reply via email to