Sean Rowe
Sean Rowe

Reputation: 33

How do I insert the same line into multiple files using VIM

I would like to insert lines across multiple files the same way i can search and replace across multiple files. For example, in order to search and replace the same string across 4 files I would do this (with multiple files open):

:115s/fidget/widget/|:wn|:115s/fidget/widget/|:wn|:115s/fidget/widget/|:wn|:115s/fidget/widget/|:wq

How would I insert a line instead of search & replace?

Upvotes: 3

Views: 1412

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45117

Insert a line by doing a replace like so:

:115/$/\rfoo/

However lets kick this up a notch with :windo:

:windo 115s/$/\rfoo/

:windo {cmd} will execute {cmd} on each window.

Substitutions not your thing or have your text in a register? Then use :put. Example of putting a line after line 115 from register "a:

:windo 115put a

Have more a bunch of files and don't want to be bothered with windows? Then use :args and :argdo`.

:args `find . -name '*.foo'
:argdo 115put a

There are a few related Vimcasts episodes that might be of intrest:

For more help see:

:h :s
:h :windo
:h :pu
:h :args
:h :argdo

Upvotes: 4

Related Questions