简体   繁体   English

Vim 在整个项目中查找和替换多行

[英]Vim find and replace multiple lines in whole project

I have code like this:我有这样的代码:

        item = QStandardItem(self.list.icon, name)
        self.list.model.appendRow(item)
        self.list.model.sort(0)

And I want to replace it with this:我想用这个替换它:

        add_database(self.list, name)

And I want to do it in my whole project (ie its directory).我想在我的整个项目(即它的目录)中做到这一点。 I use fzf and ferret vim plugins, can they help me somehow?我使用fzfferret vim 插件,它们能以某种方式帮助我吗?

  1. Open required files in vim with args command:使用 args 命令打开 vim 中所需的文件:

    args project_dir/**/*.py args project_dir/**/*.py

  2. Execute substitution on all opened files:对所有打开的文件执行替换:

argdo %s;\(\s*\)item = QStandardItem(self.list.icon, name)\n\1self.list.model.appendRow(item)\n\1self.list.model.sort(0);\1add_database(self.list, name);g

It preserves indentation which is important in Python, as Kent noticed.正如 Kent 所注意到的,它保留了在 Python 中很重要的缩进。

\n is new line symbol here. \n是这里的换行符。

\1 is group recall. \1是组召回。

See help topics for more info:有关更多信息,请参阅帮助主题:

:h :args
:h :argdo

If you use ripgrep, you can do this from the commandline: rg -l 'my_search' | xargs sed -i '' -e 's/my_search/my_new_value/'如果您使用 ripgrep,您可以从命令行执行此操作: rg -l 'my_search' | xargs sed -i '' -e 's/my_search/my_new_value/' rg -l 'my_search' | xargs sed -i '' -e 's/my_search/my_new_value/'

Or if you want an in-vim solution, this will also work: :cfdo %s/my_search/replacement/gc and :cdo update to update.或者,如果您想要一个 in-vim 解决方案,这也可以: :cfdo %s/my_search/replacement/gc:cdo update进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM