How to insert the same line into multiple files using VIM
I would like to insert lines into multiple files in the same way that I can search and replace multiple files. For example, to search and replace the same line across 4 files, I would do this (when opening multiple files):
:115s/fidget/widget/|:wn|:115s/fidget/widget/|:wn|:115s/fidget/widget/|:wn|:115s/fidget/widget/|:wq
How would I insert a string instead of search and replace?
source to share
Insert the line by doing the replacement like so:
:115/$/\rfoo/
However, let's ditch that with :windo
:
:windo 115s/$/\rfoo/
:windo {cmd}
will execute {cmd}
in every window.
Replacements are not your thing or your text in the registry? Then use :put
. An example of placing a line after line 115 from a register "a
:
:windo 115put a
Do you have more files and don't want to be disturbed by windows? Then use :args
and: argdo`.
:args `find . -name '*.foo'
:argdo 115put a
There are several Vimcasts related episodes that might be interesting:
- Meet arglist
- Filling in arglist
- Usage: argdo to modify multiple files
- Search and replace throughout the project
For more help see:
:h :s
:h :windo
:h :pu
:h :args
:h :argdo
source to share