Editing multiple files simultaneously with Vim

2019-03-16 08:31发布

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

标签: vim vi
2条回答
Root(大扎)
2楼-- · 2019-03-16 09:22

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.

查看更多
beautiful°
3楼-- · 2019-03-16 09:24

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

查看更多
登录 后发表回答