4

Referencing Post:

https://stackoverflow.com/questions/22206332/vim-ag-search-and-replace-globally-and-within-a-directory

Specifically:

:Ag foo
:Qfdo s/foo/bar/c

I type Ag miwt-os, I get this:

enter image description here

Which is the terminal, and I am lost her, how do I now get the results into the quckfix to run it through Qfdo?

2
  • 4
    Newer versions of vim 7.4.858+ provide a native version of :Qfdo via :cdo. Feb 16, 2016 at 1:25
  • 1
    @PeterRincker, I use neovim, it has it, so thanks to your comment, my .nvimrc is now 60 lines less. Feb 16, 2016 at 15:29

4 Answers 4

6

You don't need a plugin for using The Silver Searcher — or any other grep alternative — in Vim:

set grepprg=ag\ --nogroup\ --nocolor\ --ignore-case\ --column\ --vimgrep
set grepformat=%f:%l:%c:%m,%f:%l:%m

With those lines in your vimrc you can simply do :grep miwt-os to populate the quickfix list with matching lines or :lgrep miwt-os to populate the location list.

From there you can open the quickfix/location window with either :cw or :lw to take a look at the list before going further or directly use :Qfdo.

If you want the quickfix/location window to be opened automatically, add these lines to your vimrc:

augroup quickfix
    autocmd!
    autocmd QuickFixCmdPost [^l]* cwindow
    autocmd QuickFixCmdPost l*    lwindow
augroup END
3
  • You don't need a plugin to use ag with Vim, but sometimes such a plugin can be quite handy. CtrlSF is a good example. FWIW. Feb 15, 2016 at 21:07
  • 1
    I belive the --vimgrep option uses --nogroup, --nocolor, and --column by default. Feb 16, 2016 at 1:19
  • Many such plugins can search asynchronously, which is quite handy with large code bases. Aug 11, 2016 at 18:22
3

It looks like you have to :AgAdd instead of just Ag, which will add the results to quick links.

3

Use macros to do what you want. Start at the first replacement, :cr, then... (Comments just for description.)

qbq " clear b register
qa " start recording macro a
:s/old text/new text/
:cn
q " end macro
qb@a@bq " create macro b to call macro a then itself
@b " launch macro b

Watch it tear through your project.

1

But why you want to do this via Vim? It would be much easier to run ag -l pattern | xargs -n1 sed 's/pattern/replacement/g in Your shell (or at least via :!).

1
  • Good point, when working in nvim, the search results appear in quickfix window. This allows you to quickly scan through the window before going on with the replacement. For example, if notice that the search is not ignoring files in .git directory, you may want to redo the search, or it will invalidate the SHA. Thanks for the comment... Feb 16, 2016 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.