2

Normally, if you add something like

set setting+=value

into your .vimrc, the value will be appended to the existing ones. For example:

set formatoptions+=j  " => tcqj

However, it seems it doesn't work the same for status line.

set statusline=2
set statusline+=\ %{&filetype}

for the javascript file, expected status line:
[No Name]           0,0-1            All javascript

for the javascript file, actual status line:
javascript (and nothing more)

So, why using something like

set statusline=2
set statusline+=\ %{&filetype}

works the same as if we would use

set statusline=2
set statusline=
set statusline+=\ %{&filetype}

?

And how to workaround this?

2 Answers 2

5

By default, :help 'statusline' is empty and Vim uses a hardcoded routine to fill the status line. When it is non-empty, the hardcoded value is ignored, leaving you with a blank slate.

When you do any of the following, you effectively set statusline to a non-empty value which makes the hardcoded one disappear:

set statusline=foo
set statusline+=bar
set statusline^=baz

That is how statusline has been working for decades and there is no option, command-line flag, or even compile flag to change that.

The only way to augment the default status line with arbitrary information is to replicate the default status line as indicated in the relevant documentation:

Emulate standard status line with 'ruler' set  
  :set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P

In your case:

set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P
set statusline+=\ %{&filetype}
3

The reason why set += doesn’t work to append to the default status line format is that the default for the 'statusline' option is actually an empty string, and Vim renders a default status line whenever that option is empty.

Since the default is empty, set status line+=anything will modify it to be non-empty and then the default behavior will no longer trigger, so in effect you’ll get only the part that you added and not the default fields.

What complicates matters a little bit is that the code that implements the default format is actually hardcoded into the Vim binary, which is code written in C. In other words, Vim doesn’t use a default format string for the default case. The default behavior also depends on features compiled into Vim, also on the state of other options (such as ser ruler) and some of the spacing of the line numbers is hard or impossible to reproduce exactly with the format primitives available for the 'statusline' value.

Having said that, you can get a good approximation of the default, with the 'ruler' option enabled, with:

set statusline=%f\ %h%w%m%r\ %=%(%l,%c%V\ %=\ %P%)

So you can start with that setting in your vimrc and then use set += to append to it, accomplishing roughly the same effect you initially intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.