如何根据状态行中的可用空格向 vim 状态行添加可变数量的字符

How to add a variable amount of characters to a vim statusline based on available whitespace in the statusline

我已将 vim 和 运行 的自定义状态行设置为问题,当我执行 :split 时,很难看到缓冲区之间的分隔,因为方式我设置好了。我现在想添加一个字符作为左侧文本与右侧文本之间的分隔边框。在左边,我有一个文件名及其路径(长度会改变),在右边我有一些关于缓冲区编号等的信息。我不能对字符进行硬编码,因为有时文件路径太长时它会被截断。我希望能够调用一个函数,该函数将 return 一个可变数量的字符,该字符基于状态行两端之间的白色 space 数量。

我想过很多办法解决这个问题,但就是不知道怎么做,网上也找不到答案。 起初,我想在整个状态行下划线,但我不知道该怎么做。

现在我添加可变数量字符的想法有所不同,我想定义一个函数,使用正则表达式读取状态行文本并用完全适合的可变数量的字符替换该行给出白色space.

我不知道该怎么做,如果有任何帮助,我将不胜感激。 此外,我还想让状态栏的某个元素根据我的光标所在的缓冲区改变颜色,这样更容易找到正确的 window,我也找不到办法做到这一点并且将不胜感激指向正确方向的指示。

set laststatus=2
set statusline=
set statusline+=%2*\ %l
set statusline+=\ %*
set statusline+=%1*\ ‹‹
set statusline+=%1*\ %t\ %*
set statusline+=%1*\ ››
set statusline+=%1*\ %m
set statusline+=%3*\ %F
set statusline+=%{AddSeperator()}
set statusline+=%=
set statusline+=%3*\ ‹‹
set statusline+=%3*\ %{strftime('%R',getftime(expand('%')))}
set statusline+=%3*\ ::
set statusline+=%3*\ %n
set statusline+=%3*\ ››\ %*

I just do not know how to do it and I cannot find answers online

寻找答案的最佳地点是Vim的帮助系统。在本例中为 :help 'statusline'。完成后,您可以简单地google 为其他人的工作学习实例。 A relevant topic on SO, an inspirational vimrc. And, of course ;-), here is my status line.

I cannot hard code a character because sometime when the file path is too long it gets cut off.

这意味着您必须改为剪切文件路径。然后您可以添加一个 space 以确保至少有一个:

set statusline+=%3*\ %<%F
set statusline+=\ %=

如果您不喜欢 "plain spaces",请阅读 :help 'fillchars'

I also wanted to make a certain element of the status bar change color based on which buffer my cursor is in so it is easier to find the correct window

只需对某些元素使用默认颜色 (%*),它就会自动在 StatusLineStatusLineNC 之间切换。与默认状态行的方式相同。如果您不喜欢当前配色方案中的 StatusLine,您可以使用 :highlight 命令强制更改它。但请注意 StatusLine 也用于 WildMenu.

At first, I had an idea of just underlining the whole status line but I do not know how to do this.

您没有在 Vim 中加下划线 "some text"。您改用 "underlined" 颜色组。排序 :hi User1 gui=underline guifg=... guibg=... 等等