如何在 Vim 的目录中递归搜索和替换? [英] How can I search and replace recursively in a directory in Vim?

查看:45
本文介绍了如何在 Vim 的目录中递归搜索和替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了 Vim 的替代命令...

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

:%s/replaceme/replacement/gi

还有 vimgrep...

And vimgrep...

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

有没有办法将它们组合起来,以便对目录下的所有文件进行替换?

Is there a way to combine them in order to do a replacement across all the files under a directory?

推荐答案

尽管我是 Vim 用户,但我通常使用 findsed事物.sed 的正则表达式语法类似于 Vim(它们都是 ed 的后代),但并不完全相同.使用 GNU sed,您还可以使用 -i 进行就地编辑(通常 sed 将修改后的版本发送到标准输出).

Even though I'm a Vim user, I generally use find and sed for this sort of thing. The regex syntax for sed is similar to Vim's (they're both descendants of ed), though not identical. With GNU sed you can also use -i for in-place edits (normally sed emits the modified version to stdout).

例如:

find project -name '*.rb' -type f -exec sed -i -e 's/regex/replacement/g' -- {} +

逐个:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆