Suppose I've copied a bunch of lines of boilerplate code that I need to copy multiple times over, and then change in two separate places, in the same way:
it 'includes sessions#new' do
expect(get: '/sign_in').to route_to('devise/sessions#new')
end
so I'm gonna copy this seven or eight times, and I need to change sessions#new
on both lines. So if I go through and change the first line of each statement manually, I end up with this:
it 'includes sessions#new' do
expect(get: '/sign_in').to route_to('devise/sessions#new')
end
it 'includes sessions#destroy' do
expect(get: '/sign_in').to route_to('devise/sessions#new')
end
it 'includes passwords#new' do
expect(get: '/sign_in').to route_to('devise/sessions#new')
end
it 'includes passwords#create' do
expect(get: '/sign_in').to route_to('devise/sessions#new')
end
Now, I'd like to change all the second-line instances of sessions#new
to the corresponding string from the previous line.
I know I can do this with a long, convoluted multi-line substitution command:
:%s/^\( it 'include \)\(\w\+#\w\+\)\(.*' do\n.*'\)\(\w\+#\w\+\)\(.*\)$/\1\2\3\2\5
What I'd like to know is if it's possible to do something comparable and perhaps a little more compact with the :global
command, like this:
:g/includes \(\w\+#\w\+\)/+1s/sessions#new/\1
Unfortunately, the above command doesn't work, because the \1
capture group isn't available to the :g
command (and anyway, the :s
command probably thinks it's for it).
Is there any way to get this done, or am I stuck with Option 1?