how to open multiple files in vim after vimgrep
Asked Answered
M

2

2

I'm using gvim. Using vimgrep on current directory to find text across *.sql files. As it searches files, it just shows me file name at a time and in the end opens one file up.

Is it possible to open all files as tabs? Basically I want to open all files because I want to replace the 'vimgrepped' pattern with a some other text.

Mike answered 2/12, 2009 at 5:34 Comment(4)
This is a little hard, because the quickfix list stores locations, not just files.Sunder
so should I just search and replace on multiple files? and forget about opening them..Mike
I still need a list of file names that got affected..Mike
You could use :cw to open the list in a window, then extract the filenames.Sunder
R
1

To automate actions on the QuickFix list locations, I have written a command similar to :bufdo or :windo that executes a command for each item.

command! -nargs=+ Qfixdo call QuickFixDo(<q-args>)
function! QuickFixDo(cmd)
    let bufnam = {}
    for q in getqflist()
        let bufnam[q.bufnr] = bufname(q.bufnr)
    endfor  
    for n in keys(bufnam)
        exe 'buffer' n
        exe a:cmd
        update
    endfor
endfunction

Using the function one can open all files mentioned in the QuickFix list by the following command.

:Qfixdo tab sp

In addition, it is possible to repeat the substitution itself the same way.

:Qfixdo %s/pattern/string/
Repertoire answered 31/7, 2011 at 10:39 Comment(5)
I like this solution, but please give credit where due.Yama
@nelstrom: Wow! It is interesting to see how thinking the Vim way could produce almost identical solutions to similar problems. Nevertheless, I came up with this function myself, so the credit for this particular answer is not due to Al, even though the code is indeed very similar.Repertoire
@ib my apologies. It seems daft that Vim doesn't have some kind of quickfixdo built in, considering that it has bufdo, windo, tabdo and argdo.Yama
@nelstrom: Don't mention it. Totally agree about the absence of a built-in command for repeating a command on the QuickFix list files. In fact, that is exactly the reason why Vim users would reinvent it over and over.Repertoire
@nelstrom: By the way, searching similar solutions on Stack Overflow (after reading your comment), I found your idea of setting args to the QuickFix list files and using argdo. That's a very elegant workaround!Repertoire
M
1

found this plugin pretty helpful in this regard.

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

Mike answered 2/12, 2009 at 6:17 Comment(0)
R
1

To automate actions on the QuickFix list locations, I have written a command similar to :bufdo or :windo that executes a command for each item.

command! -nargs=+ Qfixdo call QuickFixDo(<q-args>)
function! QuickFixDo(cmd)
    let bufnam = {}
    for q in getqflist()
        let bufnam[q.bufnr] = bufname(q.bufnr)
    endfor  
    for n in keys(bufnam)
        exe 'buffer' n
        exe a:cmd
        update
    endfor
endfunction

Using the function one can open all files mentioned in the QuickFix list by the following command.

:Qfixdo tab sp

In addition, it is possible to repeat the substitution itself the same way.

:Qfixdo %s/pattern/string/
Repertoire answered 31/7, 2011 at 10:39 Comment(5)
I like this solution, but please give credit where due.Yama
@nelstrom: Wow! It is interesting to see how thinking the Vim way could produce almost identical solutions to similar problems. Nevertheless, I came up with this function myself, so the credit for this particular answer is not due to Al, even though the code is indeed very similar.Repertoire
@ib my apologies. It seems daft that Vim doesn't have some kind of quickfixdo built in, considering that it has bufdo, windo, tabdo and argdo.Yama
@nelstrom: Don't mention it. Totally agree about the absence of a built-in command for repeating a command on the QuickFix list files. In fact, that is exactly the reason why Vim users would reinvent it over and over.Repertoire
@nelstrom: By the way, searching similar solutions on Stack Overflow (after reading your comment), I found your idea of setting args to the QuickFix list files and using argdo. That's a very elegant workaround!Repertoire

© 2022 - 2024 — McMap. All rights reserved.