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)?
Perfect use case for the
\zsatom in vim’s regex. This tells vim to ignore anything before (and including)\zswhen doing a replacement.A better explaination of
\zscan be found at:help /\zs.