├── plugin └── qargs.vim └── README.md /plugin/qargs.vim: -------------------------------------------------------------------------------- 1 | command! -nargs=0 -bar Qargs execute 'args ' . s:QuickfixFilenames() 2 | 3 | " Contributed by "ib." 4 | " http://stackoverflow.com/questions/5686206/search-replace-using-quickfix-list-in-vim#comment8286582_5686810 5 | command! -nargs=1 -complete=command -bang Qdo call s:Qdo(, ) 6 | 7 | function! s:Qdo(bang, command) 8 | if exists('w:quickfix_title') 9 | let in_quickfix_window = 1 10 | cclose 11 | else 12 | let in_quickfix_window = 0 13 | endif 14 | 15 | arglocal 16 | exe 'args '.s:QuickfixFilenames() 17 | exe 'argdo'.a:bang.' '.a:command 18 | argglobal 19 | 20 | if in_quickfix_window 21 | copen 22 | endif 23 | endfunction 24 | 25 | function! s:QuickfixFilenames() 26 | " Building a hash ensures we get each buffer only once 27 | let buffer_numbers = {} 28 | 29 | " if the current window has a local quickfix list (a location list) 30 | " then use it, otherwise use the global quickfix list 31 | let quickfix_items = getloclist(0) 32 | if len(quickfix_items) == 0 33 | let quickfix_items = getqflist() 34 | endif 35 | 36 | for quickfix_item in quickfix_items 37 | let buffer_numbers[quickfix_item['bufnr']] = bufname(quickfix_item['bufnr']) 38 | endfor 39 | 40 | return join(map(values(buffer_numbers), 'fnameescape(v:val)')) 41 | endfunction 42 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A Vim plugin for executing operations on the files in the quickfix list. 2 | 3 | If the current window has a local quickfix list (a location list), then 4 | it's used instead of the global quickfix list, otherwise the global 5 | quickfix list is used. 6 | 7 | ## Qdo command 8 | 9 | The plugin adds a `:Qdo` command, which executes a command once for each file in the quickfix list. 10 | 11 | Usage example: 12 | 13 | :GitGrep foo.*bar 14 | :Qdo %s/foo.*bar/baz/g | update 15 | 16 | This will replace text and save the buffers for each file found by GitGrep in the previous search. 17 | 18 | ## Qargs command 19 | 20 | The plugin also adds a `:Qargs` command, which populates the argument list from the files in the quickfix list. Each file is only added once, even if there are multiple lines for the same file in the quickfix list. 21 | 22 | Usage example: 23 | 24 | :args *.txt 25 | :vimgrep /foo/g ## 26 | :Qargs 27 | :argdo %s/foo/bar/ge 28 | :argdo update 29 | 30 | ## References 31 | 32 | For an step-by-step guide and explanation, refer to the article [Project Wide Search And Replace In Vim With Qdo](http://thepugautomatic.com/2012/07/project-wide-search-and-replace-in-vim-with-qdo/). 33 | 34 | For a video tutorial of search and replace strategies, and the original code which this plugin is based on, refer to [ 35 | Project-wide find and replace](http://vimcasts.org/episodes/project-wide-find-and-replace/). 36 | 37 | For a description of the need and the approach used by this solution, refer to the question [Search & replace using quickfix list in Vim](http://stackoverflow.com/a/5686810/128850). 38 | --------------------------------------------------------------------------------