Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Referencing Post:

http://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?

share|improve this question
4  
Newer versions of vim 7.4.858+ provide a native version of :Qfdo via :cdo. – Peter Rincker Feb 16 at 1:25
1  
@PeterRincker, I use neovim, it has it, so thanks to your comment, my .nvimrc is now 60 lines less. – user1135541 Feb 16 at 15:29
up vote 6 down vote accepted

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
share|improve this answer
    
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. – Sato Katsura Feb 15 at 21:07
1  
I belive the --vimgrep option uses --nogroup, --nocolor, and --column by default. – Peter Rincker Feb 16 at 1:19

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

share|improve this answer

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.

share|improve this answer

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 :!).

share|improve this answer
    
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... – user1135541 Feb 16 at 15:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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