状态栏中的VIM行计数有千位分隔符?(VIM line count in status bar with thousands separator?)

编程入门 行业动态 更新时间:2024-10-10 09:23:16
状态栏中的VIM行计数有千位分隔符?(VIM line count in status bar with thousands separator?)

是否可以在VIM状态栏中显示具有数千个分隔符的行数,最好是自定义数千个分隔符?

例:

set statusline=%L

应该导致“1,234,567”而不是“1234567”。

Is it possible to display the line count in the VIM status bar with thousands separators, preferably custom thousands separators?

Example:

set statusline=%L

should lead to "1,234,567" instead of "1234567".

最满意答案

我找到了一种方法,但它看起来有点疯狂:

set statusline=%{substitute(line('$')\,'\\d\\zs\\ze\\%(\\d\\d\\d\\)\\+$'\,'\,'\,'g')}

第一轮反斜杠只是用于set (我必须转义,并且\本身)。

我实际上设置选项的是这个字符串:

%{substitute(line('$'),'\d\zs\ze\%(\d\d\d\)\+$',',','g')}

作为格式字符串,此行包含一个格式代码,即%{...} 。 ...所有内容都被评估为表达式,并将结果替换回来。

我正在评估的表达式是(添加了空格(如果我将它们添加到真实代码中,我将不得不再次将它们转义为set ,强制更多反斜杠)):

substitute(line('$'), '\d\zs\ze\%(\d\d\d\)\+$', ',', 'g')

这是对substitute函数的调用。 参数是源字符串,正则表达式,替换字符串和标志列表。

我们开始的字符串是line('$') 。 此调用返回当前缓冲区中的行数(或者更确切地说是缓冲区中最后一行的编号)。 这是%L通常显示的内容。

我们正在寻找的搜索模式是\d(\d\d\d)+$ (删除了特殊的vim疯狂),即一个数字后跟一个或多个3位数组,后跟字符串的结尾。 分组在vim中拼写为\%( \) ,“1或更多”为\+ ,这给我们\d\%(\d\d\d\)\+$ 。 魔法的最后一点是\zs\ze 。 \zs设置匹配字符串的开头; \ze结束了。 这就好像在\zs之前的所有内容都是一个后视模式,而\ze之后的所有内容都是一个预见模式。

这相当于:我们正在寻找源字符串中前面有一个数字并且后面跟着正好N个数字的每个位置(其中N是3的倍数)。 这类似于从右侧开始向左,每次跳过3位数。 这些是我们需要插入逗号的位置。

这就是替换字符串: ',' (逗号)。 因为我们匹配长度为0的字符串,所以我们有效地插入源字符串(通过替换'' with ',' )。

最后, g标志表示要对所有匹配执行此操作,而不仅仅是第一个匹配。

TL; DR:

line('$')给出了行数 substitute(..., '\d\zs\ze\%(\d\d\d\)\+$', ',', 'g')在我们想要的地方添加逗号 %{ }允许我们将任意表达式嵌入到statusline

I've found a way but it looks a bit crazy:

set statusline=%{substitute(line('$')\,'\\d\\zs\\ze\\%(\\d\\d\\d\\)\\+$'\,'\,'\,'g')}

The first round of backslashes is just for set (I have to escape , and \ itself).

What I'm actually setting the option to is this string:

%{substitute(line('$'),'\d\zs\ze\%(\d\d\d\)\+$',',','g')}

As a format string, this line contains one formatting code, which is %{...}. Everything in ... is evaluated as an expression and the result substituted back in.

The expression I'm evaluating is (spaces added (if I had added them to the real code, I would've had to escape them for set again, forcing yet more backslashes)):

substitute(line('$'), '\d\zs\ze\%(\d\d\d\)\+$', ',', 'g')

This is a call to the substitute function. The arguments are the source string, the regex, the replacement string, and a list of flags.

The string we're starting with is line('$'). This call returns the number of lines in the current buffer (or rather the number of the last line in the buffer). This is what %L normally shows.

The search pattern we're looking for is \d(\d\d\d)+$ (special vim craziness removed), i.e. a digit followed by 1 or more groups of 3 digits, followed by the end of the string. Grouping is spelled \%( \) in vim, and "1 or more" is \+, which gives us \d\%(\d\d\d\)\+$. The last bit of magic is \zs\ze. \zs sets the start of the matched string; \ze sets the end. This works as if everything before \zs were a look-behind pattern and everything after \ze were a look-ahead pattern.

What this amounts to is: We're looking for every position in the source string that is preceded by a digit and followed by exactly N digits (where N is a multiple of 3). This works like starting at the right and going left, skipping 3 digits each time. These are the positions where we need to insert a comma.

That's what the replacement string is: ',' (a comma). Because we're matching a string of length 0, we're effectively inserting into the source string (by replacing '' with ',').

Finally, the g flag says to do this with all matches, not just the first one.

TL;DR:

line('$') gives us the number of lines substitute(..., '\d\zs\ze\%(\d\d\d\)\+$', ',', 'g') adds commas where we want them %{ } lets us embed arbitrary expressions into statusline

更多推荐

本文发布于:2023-07-30 14:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338766.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栏中   状态   分隔符   VIM   有千位

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!