0

This is my statusline setting:

:set laststatus=2
:set statusline=%{GetMode()}\ %n\ %#StatusLineY#%m%*\%#StatusLineR#%r%*\ %t\ [%o\ %l\,%c\ %p%%\]

and this is the GetMode() functiom

function! GetMode()
    let ms=mode()
    if ms == 'n'
        return 'NORMAL '
    elseif ms == 'i'
        return 'INSERT '
    elseif ms == 'v' || ms == 'V' 
        return 'VISUAL '
    elseif ms == 's' || ms == 'S'
        return 'SELECT '
    elseif ms == 'R' ||  ms == 'Rv'
        return 'REPLACE'
    elseif ms == '!' 
        return 'SHELL  '
    endif
endfunction

This setting works well and now I would like to have different colors for the stringsreturned by GetMode() function (and not the whole statusline).

EDIT2 So for example blue foreground color for 'NORMAL', yellow for 'INSERT' etc. Is it possible to achieve this ? If so, how ?

EDIT1 According to comments given below, I tried to add the autogroup statusline as follows:

augroup statusline
autocmd!
autocmd VimEnter,ColorScheme * hi User1 guibg=#0a0a0a guifg=#0000ff
augroup END

and applying this small change to GetMode:

let ms=mode()
if ms == 'n'
   return %1*NORMAL\ %*

But this generates an E15 INvalid expression error.

6
  • 2
    Check :h statusline and :h hl-User1..9. Read the whole parts of :h statusline, everything is there.
    – 3N4N
    Apr 25, 2019 at 8:51
  • I already tried to modify the GetMode function to return the highlighted function according to that but it doesn't work.
    – xyx
    Apr 25, 2019 at 8:55
  • Yes. I added the test to the original post. As you can see, in my case it doesn't work well.
    – xyx
    Apr 25, 2019 at 9:21
  • No, you need to include the %1* in the set statusline part. It doesn't have any meaning otherwise. In my example in the link, you can see, I used %1* and %2* in the ex-command set statusline as it is suggested in :h statusline. That is to say, use set stl=%1*%{get_mode()}%0*
    – 3N4N
    Apr 25, 2019 at 9:22
  • 1
    Your function needs to return a string, so ' %1*NORMAL %*' or whatever. Apr 25, 2019 at 10:49

1 Answer 1

0

OK so I post here my own solution:

function! ColorUser1ToGreen()
        hi User1 guifg=#00ff00 guibg=#0a0a0a gui=bold
endfunction

function! ColorUser1ToLightBlue()
        hi User1 guifg=#9494ff  guibg=#0a0a0a gui=bold
endfunction

function! ColorUser1ToYellow()
        hi User1 guifg=#ffff00 guibg=#0a0a0a gui=bold
endfunction

function! ColorUser1ToOrange()
        hi User1 guifg=#ffa239 guibg=#0a0a0a gui=bold
endfunction

function! ColorUser1ToPurple()
        hi User1 guifg=#a683ff guibg=#0a0a0a gui=bold
endfunction

function! GetStatusLineMode()
    let ms=mode()
    if ms == 'n'
        call ColorUser1ToLightBlue()
        return 'NORMAL  '
    elseif ms == 'i'
        call ColorUser1ToYellow()
        return 'INSERT  '
    elseif ms == 'v' || ms == 'V' 
        call ColorUser1ToOrange()
        return 'VISUAL  '
    elseif ms == 's' || ms == 'S'
        call ColorUser1ToOrange()
        return 'SELECT  '
    elseif ms == 'R' ||  ms == 'Rv'
        call ColorUser1ToPurple()
        return 'REPLACE '
    elseif ms == '!'
        call ColorUser1ToPurple()
        return 'SHELL   '
    else
        call ColorUser1ToGreen()
    endif

endfunction

:set laststatus=2
:set statusline=%1*%{GetStatusLineMode()}%*\ %n\ %#StatusLineY#%m%*\%#StatusLineR#%r%*\ %t\ [%o\ %l/%L(%p%%)\ %c\ \]

Perhaps there are better solutions, but this fit my preferences.

2
  • 4
    calling a :hi command while evaluating the statusline is not a good thing to do. This might cause additional redraws. Better, use a dedicated User highlighting for each mode, so you can initialize the User highlighting only once and do not need to re-create them over and over Apr 25, 2019 at 10:55
  • How many User colors can we define? Docs say that only 10 of them - is there any hack for creating more? Also, having %#someHIname# is not sufficient because to actually change statusline, every time is has to be defined from scratch, using new colors. How to hack this properly and cleanly?
    – piotao
    Oct 27, 2020 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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