vimencodingutf-8statusline

How to change color in statusline if current file has no utf8 fileencoding?


I want to realize a different color in my statusline if a file has no utf8 encoding.

This is what I use now:

set statusline+=%3*\ F:%{&fileencoding?&fileencoding:&fileencoding} 

hi User3 guifg=#292b00  guibg=#f4f597

This is what I want to realize:

set statusline+=%{Fenc()}*\ F:%{&fileencoding?&fileencoding:&fileencoding}
function! Fenc()
    if &fenc !~ "utf-8"
        return "4"
    else
        return "3"
    endif
endfunction

hi User3 guifg=#292b00  guibg=#f4f597
hi User4 guifg=#ff0000  guibg=#f4f597 

Why doesn't this work?


Solution

  • first of all, in your code:

    %{&fileencoding?&fileencoding:&fileencoding} 
    

    makes no sense, it is like, if a is there, I write a, otherwise I write a anyway.

    I guess you want to have &fenc?&fenc:&enc

    I don't think you can evaluate the function and then put with % together with set stl, but you can build your function in this way:

    hi User3 ....
    hi User4 ....
    function! MkStatusLine()
        if &fenc == "utf-8"     
            set statusline=%4*
        else
            set statusline=%3*
        endif
        set statusline+=Here you made your magic status line info text
    endfunction
    

    then call the function when you load buffer.

    Edit

    add how it worked in terminal:

    enter image description here