vimstatusline

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


I have made my custom status line for vim and I ran into a problem, When I do :split, it becomes very hard to see the separation between the buffers because of the way I set it up. I now want to add a character to act as a separating border between the text on the left side to the right side. On the left, I have a file name and its path(Which alters in length), on the right I have some information about the buffer number and such. I cannot hard code a character because sometime when the file path is too long it gets cut off. I want to be able to call a function that will return a variable amount of a character based on the amount of white space I have between the two ends of the status line.

I have thought of multiple ways to solve this but I just do not know how to do it and I cannot find answers online. At first, I had an idea of just underlining the whole status line but I do not know how to do this.

Now my idea for adding a variable number of characters is a different, I want to define a function that uses regex to read the status line text and replace the line with a variable amount of characters that would fit perfectly in the given whitespace.

I do not know how to do this and I would appreciate any help. In addition, 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, I could not find a way to do this either and a pointer in the right direction will be appreciated.

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*\ ››\ %*

Solution

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

    The best place to look for answers is Vim's help system. In this case it's :help 'statusline'. After finishing it, you may simply google for other people's work to learn by example. 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.

    That means you have to cut a file path instead. Then you can add a single space to make sure there's at least one:

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

    If you don't like "plain spaces", read :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

    Just use the default color (%*) for some element and it will switch between StatusLine and StatusLineNC automatically. The same way as it is for the default status line. If you don't like StatusLine in your current colorscheme, you can forcefully change it with :highlight command. But note that StatusLine is also used for WildMenu.

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

    You don't underline "some text" in Vim. You use "underlined" color groups instead. Sort of :hi User1 gui=underline guifg=... guibg=... etc.