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

如何在vim中执行搜索/替换,同时保留通配符内容?,vim,replace,Vim,Replace,这就是我的意思- 假设我的档案里都有这些 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 d

这就是我的意思-

假设我的档案里都有这些

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\u hash调用,因此我得到:

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
问题是,如何在搜索过程中保留实际查询,并替换出现的内容…某些内容。。。是吗?

您可以使用全局命令进行筛选

: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.
可以使用“全局”命令进行筛选

: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.
vim的正则表达式中\zs原子的完美用例。这告诉vim在进行替换时忽略\z之前和包括\z在内的任何内容

:%s/\.execute(.\{-})\.each\zs_hash/(:as => :hash)/
有关\zs的更好解释,请访问:help/\zs。

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

:%s/\.execute(.\{-})\.each\zs_hash/(:as => :hash)/
有关\zs的更好解释,请访问:help/\zs