user564376
user564376

Reputation:

Include color string in statusline

I have some function defined as

function! myfunc()
    let s = 'hello world'
    return s
endfunction

and I can include this in my status line as statusline=%{myfunc()}, which nicely prints 'hello world'. I can also color it as statusline=%#mycolor#%{myfunc()} where mycolor is a color I've defined.

Now I want to color each of the words separately, so I redefine my function as

function! myfunc()
    let s = '%#mycolor1#hello %#mycolor2#world'
    return s
endfunction

However, when I set this in the status line, the output is simply the literal string "%#mycolor1#hello %#mycolor2#world", whereas I want hello to be colored according to mycolor1 and world colored according to mycolor2.

How do I go about doing this?

Upvotes: 4

Views: 804

Answers (1)

Herbert Sitz
Herbert Sitz

Reputation: 22226

I think this vim utility may partly answer your question:

http://www.vim.org/scripts/script.php?script_id=3383

So it looks like you can't have color changes within your 'myfunc' function. But you can get color changes by assigning statusline using an exec command, e.g.:

:let sl_statement = 'set statusline=%#' . color1highlight .
                 \   '#%{myfunc1()}%#' . color2hl . '#%{myfunc2()}'
:exec sl_statement

Upvotes: 3

Related Questions