请教个 vim 的问题

2016-07-05 22:59:24 +08:00
 NK

以下面文本为例

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

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

2357 次点击
所在节点    Vim
5 条回复
kotomi
2016-07-06 00:44:38 +08:00
try this command:
:g /^start/+1,/^end/-1 <

general commands for block operation:
:g /pattern operation
:g /pattern1,/pattern2 operation
wwulfric
2016-07-06 10:02:50 +08:00
:%s/\(start\)\s\n\s\+/\1\r/g

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

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

特殊点:

1. vim 里很多正则的特殊符号需要转义,比如 ()+ 等
2. 匹配换行用 \n ,替代换行用 \r
livelazily
2016-07-08 22:03:38 +08:00
NK
2016-07-13 21:27:07 +08:00
@kotomi 谢谢
NK
2016-07-13 21:27:47 +08:00
@livelazily 这个插件赞

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/290521

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX