Just want to insert a word or text at the beginning of range of lines, or the entire file. How do it in vim?
Another good, more interactive method is to use visual block mode (ctrl-V) to select the first column, then I to insert - type what you need, hit escape, and it'll be added to all the lines. This of course works in any column, not just the first, and you can also use A to append, or c or d . Err....
The command:
:%s/^/foo: /
...inserts foo:
(including a trailing space) at the beginning of each line.
For a range you can use line numbers:
:10,20s/^/foo: /
...will do it for line 10 to 20.
My preferred way to do it for a range of lines is this: move the cursor to the first line of the range, then enter ma
to set the marker a
to the current line. Move to the end of the range and enter
:'a,.s/^/foo: /
I've become much more accustomed to use visual blocks for this kind of thing:
gg
for first line, ^
to move to the start)<C-V>
Ifoo<ESC>
That would insert foo
at the start of each line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With