如何隐藏空行?
How to hide empty lines?
我曾经使用位置列表 (:lvimgrep) 来显示缓冲区的内容,但最近我发现折叠是一种有趣的替代方法。我正在使用 foldmethod=expr
和 foldexpr=getline(v:lnum)=~'.'?1:0
选项。所有非空行(a.k.a.段落)折叠起来,用空行分隔。在这里你可以看到 original file and folded. The advantages of this compared to location list is it shows amount of lines and there is no need for additional buffer. However, for readability it would be better to remove blank lines between folds and I do not know how to do it. It is possible with manual folding but if we include empty lines in foldexpr
, vim will merge all this paragraphs into one fold. How to separate them? Here is 它应该是什么样子。
您可以像这样在前面的段落中添加空行:
:set foldexpr=strlen(getline(v:lnum))==0?'=':strlen(getline(v:lnum-1))?1:'>1'
如果当前行为空,则使用上一行的折叠级别 (=
)。
否则,检查前一行:如果它是空的,这一定是一个新段落的开始。使用 >1
创建一个新的 1 级折叠。否则这必须是现有段落的一部分;为其指定折叠级别 1
.
我曾经使用位置列表 (:lvimgrep) 来显示缓冲区的内容,但最近我发现折叠是一种有趣的替代方法。我正在使用 foldmethod=expr
和 foldexpr=getline(v:lnum)=~'.'?1:0
选项。所有非空行(a.k.a.段落)折叠起来,用空行分隔。在这里你可以看到 original file and folded. The advantages of this compared to location list is it shows amount of lines and there is no need for additional buffer. However, for readability it would be better to remove blank lines between folds and I do not know how to do it. It is possible with manual folding but if we include empty lines in foldexpr
, vim will merge all this paragraphs into one fold. How to separate them? Here is 它应该是什么样子。
您可以像这样在前面的段落中添加空行:
:set foldexpr=strlen(getline(v:lnum))==0?'=':strlen(getline(v:lnum-1))?1:'>1'
如果当前行为空,则使用上一行的折叠级别 (=
)。
否则,检查前一行:如果它是空的,这一定是一个新段落的开始。使用 >1
创建一个新的 1 级折叠。否则这必须是现有段落的一部分;为其指定折叠级别 1
.