问题
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?
回答1:
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
来源:https://stackoverflow.com/questions/5994525/include-color-string-in-statusline