Update: New official vim plugin cfilter
Since 21.8.2018 (patch: 8.1.0311) the plugin cfilter is distributed with vim in $VIMRUNTIME
. It is documented under :h cfilter-plugin
.
Load plugin cfilter when needed or load it always in your vimrc
:packadd cfilter
Using abbreviations in case of a vanilla vim this can be as short as :pa c<tab><cr>
.
Filter quickfix list with
:Cfilter DPUST
Again, using abbreviations in case of vanilla vim the command :Cfilter
can be shortened to :Cf
.
Run :cfdo
:cfdo %s/old/new/g
Original Answer
As far as I see it, :cfdo
works on the quickfix list. This will not be changed by prefixing the command with :g/DPUST/
. You could have come up with :v/DPUST/delete
and then :cfdo %s/old/new/gc
. However, the quickfix list cannot be changed easily. There are quite a few plugins which provide support for this
Following plugins allow to remove lines from the quickfix list with vim commands e.g. :v/DPUST/d
If do not want a full plugin, add following lines to your vimrc to get the command :Qfilter
" From https://github.com/dhruvasagar/dotfiles/blob/master/vim/plugin/filter_list.vim
function! s:FilterList(list, bang, pattern)
let list = deepcopy(a:list)
let [cmp, and_or] = a:bang ? ['!~#', '&&'] : ['=~#', '||']
return filter(a:list, "bufname(v:val.bufnr) " . cmp . " a:pattern " . and_or . " v:val.text " . cmp . " a:pattern")
endfunction
function! s:FilterQuickfixList(bang, pattern)
call setqflist(s:FilterList(getqflist(), a:bang, a:pattern))
endfunction
command! -bang -nargs=1 -complete=file Qfilter call s:FilterQuickfixList(<bang>0, <q-args>)
This has already been asked:
Note grep
stands for global/regular expression/print
. Questions using the term grep
or global
are referring possibly to identical tasks.
grep
command or something similar. Wouldn't it be easier to restrict thisgrep
command to the directory you want to modify instead of trying to bend:cfdo
?ripgrep
for searching. I want to search all the project files. Only based on the search result, I can decide in which directory should I replace the matches.:h :cfdo
you see thatcfdo
actually uses:cfirst
,{cmd}
,:cnfile
,{cmd}
, etc so callingcfdo
in a global command is useless, and I don't think you have a way to filter the entries of the quickfix list. I think your best bet is to use a second call toripgrep
to restrict to the directory you want to treat. Let's see if someone come up with a better solution :)