How to automat editing of several files in Vim?

前端 未结 2 784
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 14:22

I have a multitude of files in each of which I want to, say, delete lines 1 through 55, add a comment leader (e.g., //) on lines 25 through 35, and then save th

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-30 14:37

    You can accomplish this elegantly with ed, the unix line editor, the ancestor of vi.

    fix_file () {
      ed -s "$1" <<-'EOF'
        1,55d
        25,35s_.*_// &_
        wq   
    EOF
    }
    

    Now, for each file F you need, just execute fix_file F.

提交回复
热议问题