如何在 vim 中执行搜索/替换,同时保留通配符内容?

发布于 2025-01-05 15:36:32 字数 1086 浏览 2 评论 0原文

这就是我的意思 -

假设我的文件中有这些,

ActiveRecord::Base.connection.execute("select * from table1").each_hash do ..
ActiveRecord::Base.connection.execute("select * from table2").each_hash do ..
ActiveRecord::Base.connection.execute("select * from table3").each_hash do ..

client.query("select * from table1").each_hash do ..
client.query("select * from table2").each_hash do ..
client.query("select * from table3").each_hash do ..

我只想each(:as => :hash)< 替换 ActiveRecord 的 each_hash 调用/code>,所以我会得到:

ActiveRecord::Base.connection.execute("select * from table1").each(:as => :hash) do ..
ActiveRecord::Base.connection.execute("select * from table2").each(:as => :hash) do ..
ActiveRecord::Base.connection.execute("select * from table3").each(:as => :hash) do ..

并使 client.query 行不受影响。

我知道我可以使用宏,但是如何使用 vim 的搜索/替换来做到这一点?我考虑过使用这个:

%s/\.execute(.*).each_hash/ ...something... /g

问题是,如何通过搜索和替换保留实际查询(...某物...所在的地方是什么)?

Here's what I mean -

Say I have these throughout my file

ActiveRecord::Base.connection.execute("select * from table1").each_hash do ..
ActiveRecord::Base.connection.execute("select * from table2").each_hash do ..
ActiveRecord::Base.connection.execute("select * from table3").each_hash do ..

client.query("select * from table1").each_hash do ..
client.query("select * from table2").each_hash do ..
client.query("select * from table3").each_hash do ..

I want to replace only the ActiveRecord's each_hash calls with each(:as => :hash), so I'd get:

ActiveRecord::Base.connection.execute("select * from table1").each(:as => :hash) do ..
ActiveRecord::Base.connection.execute("select * from table2").each(:as => :hash) do ..
ActiveRecord::Base.connection.execute("select * from table3").each(:as => :hash) do ..

And leave the client.query rows unaffected.

I know I can use macros, but how do I do this with vim's search/replace? I thought about using this:

%s/\.execute(.*).each_hash/ ...something... /g

The question is, how do I preserve the actual query through the search and replace (what comes where ...something... is)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云胡 2025-01-12 15:36:32

vim 正则表达式中 \zs 原子的完美用例。这告诉 vim 在进行替换时忽略 \zs 之前(包括)的任何内容。

:%s/\.execute(.\{-})\.each\zs_hash/(:as => :hash)/

关于 \zs 的更好解释可以在 :help /\zs 中找到。

Perfect use case for the \zs atom in vim's regex. This tells vim to ignore anything before (and including) \zs when doing a replacement.

:%s/\.execute(.\{-})\.each\zs_hash/(:as => :hash)/

A better explaination of \zs can be found at :help /\zs.

巴黎夜雨 2025-01-12 15:36:32

您可以使用global命令进行过滤。

:g/^ActiveRecord/s/each_hash/each(:as => :hash)/g

输入 :help :g 获取帮助:

:[range]g[lobal]/{pattern}/[cmd]
            Execute the Ex command [cmd] (default ":p") on the
            lines within [range] where {pattern} matches.

You can use the global command to filter.

:g/^ActiveRecord/s/each_hash/each(:as => :hash)/g

Type :help :g to get help:

:[range]g[lobal]/{pattern}/[cmd]
            Execute the Ex command [cmd] (default ":p") on the
            lines within [range] where {pattern} matches.
无戏配角 2025-01-12 15:36:32

执行此操作的正则表达式:

:%s/\(^\(\(.*client.*\)\@!.\)*\)each.hash/\1each(:as => :hash)/g

请参阅 ^\(\(.*client.*\)\@!.\)* 部分的说明:https://vim.fandom.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches

A regex to do that:

:%s/\(^\(\(.*client.*\)\@!.\)*\)each.hash/\1each(:as => :hash)/g

See explanation for ^\(\(.*client.*\)\@!.\)* part at: https://vim.fandom.com/wiki/Search_for_lines_not_containing_pattern_and_other_helpful_searches

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文