[英]How to add a line after every few lines in vim
我想使用vim编辑器在文件中每隔3行添加一行(大约1000行)。 有人可以帮我吗?
谢谢,艾丽莎
有一个vim特定的正则表达式来做到这一点
:%s/.*\n.*\n.*\n/\0\r/g
编辑:如果你想要一个新行,只需将文本放在\\ r \\ n之前(正确的正则表达式转义,如果它包含一些正则表达式字符)
你可以使用宏 。 完整的过程如下:
qq " start recording to register q (you could use any register from a to z)
o " insert an empty line below cursor
<Esc> " switch to normal mode
jjj " move the cursor 3 lines downward
q " stop recording
然后只需移动到起始行并输入1000@q
即可执行1000次宏。
" insert a blank line every 3 lines
:%s/\v(.*\n){3}/&\r
: .............. command
% .............. whole file
s .............. replace
/ .............. start pattern that we will replace
\v ............. very magic mode, see :h very-magic
(.*\n) ......... everything including the line break
{3} ............ quantifier
/ .............. start new pattern to replace
& .............. corresponds to the pattern sought in (.*\n)
\r ............. add line break
我会这样做:
:%s/^/\=(line(".")%4==0?"\n":"")/g
如果您的要求更改为“* 每700行 * s 添加一个新的空白行 ”,则此功能可用 :)您只需更改“4”
PS如果我需要这样做,我不会在vim中这样做。 sed,awk,可以做得更简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.