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

前端 未结 7 693
抹茶落季
抹茶落季 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:15

    To answer the original question of combining vimgrep and substitute:

    :vimgrep /pattern/ ./**/files              # results go to quickfix list
    :set autowrite                             # auto-save when changing buffers
    :silent! cdo %s/replaceme/replacement/gic  # cdo draws filenames from quickfix list
    :set noautowrite
    

    Leave out the c of gic if you don't want to confirm each replacement (in which case you could also use find and sed as suggested by @LaurenceGonsalves).

    0 讨论(0)
提交回复
热议问题