10

I'd like to insert a word in multiple lines, is there any command like

:[2,3,5,7,11]s/^/word\ /g

which would insert word at the beginning of line 2, line 3, line 5, line 7 and line 11?

Here, multiple lines are not contiguous.

statox
  • 49,782
  • 19
  • 148
  • 225
user3813057
  • 1,451
  • 3
  • 17
  • 26

1 Answers1

16

Performing a substitution on several consecutive lines is pretty easy:

:2,11s/^/word /

but a range can't cover non-consecutive lines.

With a bit of creativity, though, it is entirely possible to work around that "limitation".

Indeed, you can repeat the last substitution with :& or :&& (the former will not preserve the original flags, the latter will) so you can chain substitutions pretty easily:

2s/^/word /|3&|5&|7&|11&

See :help :& and scroll around for :&&.

romainl
  • 40,486
  • 5
  • 85
  • 117