How can I search and replace recursively in a directory in Vim?

前端 未结 7 696
抹茶落季
抹茶落季 2021-01-30 02:44

I found out about Vim\'s substitute command...

:%s/replaceme/replacement/gi

And vimgrep...

:vimgrep  /findme/gj  project/**/*.r         


        
7条回答
  •  一生所求
    2021-01-30 03:13

    There's a plugin which endeavours to do just that: Vimgrep Replace.

    Oh, wait... there are others: Global Replace, EasyGrep.

    For a no plugin solution, perhaps argdo would help if you could get vimgrep's output onto the argument list (which can be set with args), but I can't seem to figure out the details. I'd be happy if somebody took the idea and improved upon it... so here it is.

    The basic idea behind the first plugin (I'd guess the others too...) is to use vimgrep first, then cycle through the matches with :cnext and apply the substitution command on each line. A function to accomplish this could be small enough to put it in .vimrc. (Maybe you can lift one from the plugins' sources?)

    (I suppose hgimenez might be on to a solution, but whether or not it's appropriate would probably depend on the number of files to be processed... The plugins should be ok regardless.)

提交回复
热议问题