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).
%*
after the string, as the docs says:h 'statusline'
(it is burried in a pretty deep paragraph)%*
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.%*
. 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.