[英]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.