6

I need to add several lines all at the same location to multiple files. I was wondering if I could possibly open all files with Vim, and only make the changes in one file for which the changes will be made in all files simultaneously. I really want to avoid opening X number of files, copying this, pasting, then repeating for each file of X files...There's gotta be a better way to do this, hopefully with vim...

Thanks! Amit

3
  • 3
    Why vim? If you have identical files (or with the exact same structure) you can script it easily in almost any language. Commented Feb 22, 2011 at 22:49
  • 1
    Oh WOW that is so freaking true. Why didn't I think of it. !@$@#$@#$#@$ Commented Feb 22, 2011 at 23:01
  • 2
    If you're super comfortable with vim and not other scripting languages, see vim's man page for the -w and -s options -- they let you record a session as a script. Commented Feb 23, 2011 at 8:12

2 Answers 2

5

You could record macro and execute it on other files. See http://www.thegeekstuff.com/2009/01/vi-and-vim-macro-tutorial-how-to-record-and-play/ for detailed tutorial.

Sign up to request clarification or add additional context in comments.

1 Comment

I checked out the tutorial, I found it very helpful, though I think I'll just quickly throw something into python to do this all for me...It'll probably be SO much easier...Thanks!
5

You can use the windo command to operate in all windows. Combine this with a substitute command and you have this (say you want to add "This is a new line." at line 2 in every file):

:windo 2s/\(.*\)/This is a new line.^M\1

Off course, as others noted, there are much better tools for this job (awk comes to mind).

2 Comments

Thank you for that. What is this awk that you speak of, or do you just mean awkward...
@Amit: I mean awk, the scripting language

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.