2023-05-29 05:00:04

Vim删除空行

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


当前回答

在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

我尝试了这一页上的一些答案,但很多都不适合我。也许是因为我在Windows 7上使用Vim(别嘲笑我,可怜可怜我吧:p)?

下面是我在Windows 7的Vim上找到的最简单的方法:

:v/\S/d

在Vim Wikia上有一个更长的答案:http://vim.wikia.com/wiki/Remove_unwanted_empty_lines

: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/^[ \t]*$/d
:v/./d

or

:g/^$/d

:%!cat -s