Editing multiple files simultaneously with Vim

﹥>﹥吖頭↗ 提交于 2019-12-31 22:41:16

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/5084919/editing-multiple-files-simultaneously-with-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!