Search and replace in Vim across all the project files

后端 未结 11 1892
我寻月下人不归
我寻月下人不归 2020-12-07 07:00

I\'m looking for the best way to do search-and-replace (with confirmation) across all project files in Vim. By \"project files\" I mean files in the current directory, some

11条回答
  •  轮回少年
    2020-12-07 07:52

    1. :grep  (or whatever you use to populate the quickfix window)
    2. :cfdo %s///g | update
    

    Step 1 populates the quickfix list with items you want. In this case, it's propagated with search terms you want to change via grep.

    cfdo runs the command following on each file in the quickfix list. Type :help cfdo for details.

    s///g replaces each term. /g means replace every occurrence in the file.

    | update saves the file after every replace.

    I pieced this together based upon this answer and its comments, but felt it deserved its own answer since it's all in one place.

提交回复
热议问题