2023-05-29 05:00:04

Vim删除空行

我可以运行什么命令来删除Vim中的空行?


当前回答

以下命令可以用来删除多个空行(将它们减少为单个空行),并保留单个空行:

:g/^\_$\n\_^$/d

其他回答

以下命令可以用来删除多个空行(将它们减少为单个空行),并保留单个空行:

:g/^\_$\n\_^$/d
:g/^\s*$/d
^ begin of a line
\s* at least 0 spaces and as many as possible (greedy)
$ end of a line

粘贴

:command -range=% DBL :<line1>,<line2>g/^\s*$/d

在你的。vimrc,然后重新启动你的vim。 如果你使用命令:5,12DBL 它将删除第5行到第12行之间的所有空行。 我认为我的答案是最好的答案!

:g/^$/d

:g将在匹配正则表达式的行上执行命令。正则表达式是'blank line',命令是:d (delete)

在vim中使用perl:

这是正确的道路。Perl -pi -e s/^\s*$//g

如何删除所有空白行 : \ n \ n % s, M ^, g (这样做多次直到所有的空行都消失了) 如何删除所有空白行留下单一的空行 : % s。\ n \ n \ n, M ^ ^ M g (重复多次) 如何删除所有空白行留下两个空行在最大, : % s。\ n \ n \ n \ n, M M ^ ^ ^ M g (重复多次)

为了输入^M,我必须在窗口中control-Q和control-M