NK
V2EX  ›  Vim

请教个 vim 的问题

  •  
  •   NK · Jul 5, 2016 · 3361 views
    This topic created in 3624 days ago, the information mentioned may be changed or developed.

    以下面文本为例

    start 
        content
    end 
    start 
        content
    end 
    start 
        content
    end 
    start 
        content
    end 
    

    如何同时将所有位于 start 和 end 之间的行缩进?更通用地讲, vim 中如何根据正则表达式匹配批量执行某个特定操作?谢谢。

    5 replies    2016-07-13 21:27:47 +08:00
    kotomi
        1
    kotomi  
       Jul 6, 2016
    try this command:
    :g /^start/+1,/^end/-1 <

    general commands for block operation:
    :g /pattern operation
    :g /pattern1,/pattern2 operation
    wwulfric
        2
    wwulfric  
       Jul 6, 2016
    :%s/\(start\)\s\n\s\+/\1\r/g

    标准语法 :s/正则匹配 /正则替换 /

    %表示匹配全文, g 表示一行中有几个就匹配几个

    特殊点:

    1. vim 里很多正则的特殊符号需要转义,比如 ()+ 等
    2. 匹配换行用 \n ,替代换行用 \r
    NK
        4
    NK  
    OP
       Jul 13, 2016
    @kotomi 谢谢
    NK
        5
    NK  
    OP
       Jul 13, 2016
    @livelazily 这个插件赞
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6202 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 02:21 · PVG 10:21 · LAX 19:21 · JFK 22:21
    ♥ Do have faith in what you're doing.