2

I'll preface by saying that I am using Airline, plytophogy/vim-virtualenv plugin and gruvbox as the Airline theme.

I am trying to change the color of some string in the status line to, say, orange. In my case that is the "@my_env" string that states my current working environment: enter image description here

Currently, the line in my .vimrc that is responsible for this text is:

let g:airline_section_c='%t @%{virtualenv#statusline()}'

Based on this question, I tried to change my .vimrc line to

let g:airline_section_c='%t %#orange#@%{virtualenv#statusline()}'

which seems to have highlighted the statusline from "@my_env" onwards: Before

It seems that using %#any_color# or even %##by itself produces the same effect.

So my question is: Is there a way to set a color for a string in the statusline?

  • I don't use airline but I know that in a statusline when you need to restore the default highlighting you can use %* after the string, as the docs says :h 'statusline' (it is burried in a pretty deep paragraph) – statox Apr 19 at 16:04
  • @statox, Adding %* after the string lead to the area after "@my_env" being highlighted. Adding %* before the string produced the same effect as in the last image. Thank you fro your help though. – Andrey Mikus Apr 19 at 16:55
  • 1
    Use airline's issue tracker. Obviously the developers would be more able to help. And to ensure @statox's comment, I also can attest %*. But obviously airline is taking the variable and doing something weird with it. I suggest you use the default statusline after configuring it yourself. Or ring up the people who are maintaining airline. – klaus Apr 19 at 18:12
  • @klaus After spending so much time trying to make these little tweaks, I think i've learned enough to make my own... I will ask the devs though. Thanks for the advice. – Andrey Mikus Apr 19 at 21:39
  • @klaus Airline maintainer here: Not sure why you think that obviously airline is taking the variable and doing something weird with it. Airline does not do anything weird here. However I am puzzled, where the orange highlighting group is defined. – Christian Brabandt Apr 21 at 8:45
1

It looks like there are actually several questions to be answered here.

1. Is there a way to set a color for a string in the statusline?

Yes there is. As the help for the statusline states, you can inject highlighting groups into the statusline by either using %#<NAME># to start using the highlighting group <NAME> or use %1*, to start using highlighting group User1. Use the numbers 1-9 to select the corresponding highlighting group User1-User9. Restore the highlighting group by using %*. Restoring in this case means, to use again the StatusLine (or StatusLineNC for non-active windows) highlighting group. (This part is important, I'll come back to this in a bit.)

However, this only works, if the actual highlighting group does exists. So you have to define the group like this:

:hi MyRedHighlightingGroup ctermfg=red ctermbg=black guifg=red guibg=black

Simply throwing magical color names into the statusline like your orange won't make it work. I guess you must have defined the orange highlighting group somewhere.

2. How to use airline and customize the different sections

In contrast to what was assumed in the comments, there is no magic going on here. But let me explain.

As you have already noticed, it is pretty easy to customize the airline sections, by simply putting the definitions into the global variable g:airline_section_X. (Use a window local variable to have this applied only to a particular window):

let g:airline_section_c='%t %#orange#@%{virtualenv#statusline()}'

So this will display the Filename, and start highlighting in orange (provided the highlighting group does actually exists) the output of the function virtualenv#statusline() until the next section.

Now airline makes heavy use of customized highlighting groups, as you can tell, if you look at the output of :hi because you'll notice many, many airline groups that all start with airline_xxx. Basically each section has its own highlighting group, plus groups that will be injected before the next section starts (the airline_x_to_airline_y groups), so that the powerline symbols look nice and show a fancy transition between each section.

So if you want to reset the highlighting group to the one that was previously used, you must not used %*, as this will switch the highlighting group to be used to StatusLine (or StatusLineNC for non-active windows), since this highlighting group is not used at all for an airline managed statusline. Instead, you probably want to reset to the actual used highlighting group (which for the mentioned section c is airline_c):

let g:airline_section_c='%t %#orange#@%{virtualenv#statusline()}%#airline_c#'

As this will correctly switch back to the highlighting group used in section c. Note, that when the window becomes inactive, airline will automatically try to use the corresponding highlighting group orange_inactive, so that needs to be defined as well.

Also note, that section c for airline is actually a bit different here for inactive windows. Because in order to distinguish inactive sections containing different buffers, airline will automatically append the buffer number to the c, else it could not use different inactive highlighting groups. So this might be an additional complexity for section c. Might be easier to use one of the other pre-defined sections.

3. Some tips on debugging airline

In any case, you can always examine the result of the defined statusline using

echo airline#statusline(<winnr>)

where <winnr> is the window number. A bit simpler even is to use :echo eval(&statusline[2:]), to see the resulting statusline option for the current window. In a similar way, you can always see the result of the tabline using :echo eval(&tabline[2:]).

(On a related note: this also means, that if the statusline wraps, or some characters are not correctly aligned, that this almost always means it is either a font incompatibility or a Vim bug or a problem between different used unicode libraries between Vim and the terminal. Airline cannot influence it, since in the end it is merely setting the statusline option).

  • This is a very comprehensive answer, thank you. From my understanding, this means that you cannot set a text color to a string without changing its entire formatting (which also includes the background), which is a bit unfortunate. The %#airline_c#' addition fixed the highlighting issue though. – Andrey Mikus Apr 26 at 23:29

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.