Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim :Ag search and replace, globally and within a directory

Tags:

vim

I am using :Ag in vim to search for patterns, but I can't figure out how to search and replace through ALL files in my project, or within a directly that I specify. How is this done?

like image 534
user3080098 Avatar asked Mar 05 '14 18:03

user3080098


1 Answers

In Vim, project-wide search and replace is at best a two-step process unless you install a plugin that abstracts those two steps for you like EasyGrep.

In its most basic form, project-wide search/replace looks like this:

:args `grep -nl foo *.js`
:argdo %s/foo/bar/c

It won't help you much with :Ag, though, because Vim doesn't have a built-in command similar to the :*do family that works on the quickfix list.

Drew Neil has a couple of screencasts, here and there, that deal with project-wide search/replace. The :Qfdo command mentionned at the bottom of the second post is specifically geared toward :vimgrep/:Ack/:Ag users. With that command and :Ag, the two-step process becomes:

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

-- EDIT --

Vim now has :cdo, :cfdo, :ldo, :lfdo.

like image 140
romainl Avatar answered Oct 14 '22 13:10

romainl