Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Editing multiple files simultaneously with Vim

Tags:

vim

vi

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

like image 542
Amit Avatar asked Feb 22 '11 22:02

Amit


2 Answers

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.

like image 184
Gleb M Borisov Avatar answered Oct 09 '22 21:10

Gleb M Borisov


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).

like image 20
Eelvex Avatar answered Oct 09 '22 23:10

Eelvex