2

I find myself more then once doing this repetitive thing:

I have something like:

do amount_foo blah_blah

I need to copy this n times, but change the foo part. I copy and paste it as many times as needed:

do amount_foo blah_blah
do amount_foo blah_blah
do amount_foo blah_blah
do amount_foo blah_blah

Then a replace on one and one line:

:'<,'>s/foo/bar
:'<,'>s/foo/baz
:'<,'>s/foo/lot

So I get:

do amount_foo blah_blah
do amount_bar blah_blah
do amount_baz blah_blah
do amount_lot blah_blah

Is there a quicker way to do the same? Is there for example a way to pass a list to replace so that one could do:

:'<,'>s/foo/\=some_magic bar, baz, lot

Easy enough to write a function for it, but there is perhaps a better way to do this natively?

user3342816
  • 300
  • 1
  • 9

1 Answers1

4

You are correct you can use a sub-replace-expression, \=, to do the list manipulation and replacement.

:let a = ['foo', 'bar', 'baz']
:%s/amount_\zsfoo/\=add(a, remove(a, 0))[-1]/

This "rotates" the array, a, in-place and uses the last position of the array as the value for the replacement of the substitution.

For more help see:

:h :s
:h range
:h /\zs
:h :s\=
:h reverse(
:h :s_flags
:h Lists
:h add(
:h remove
Peter Rincker
  • 15,854
  • 1
  • 36
  • 45