This function assigns a random colorscheme in Vim. I also want it to display a message telling what the new colorscheme is but that part isn't working.
If I call this function and then type :messages
, I see "colorscheme gruvbox", for example, in the message history. Also, if I comment out the execute
line, it displays the colorscheme name on the screen as expected.
I tried looking at the :help :execute
and :help :echom
, but haven't been able to figure out what the issue is.
function Rcolor()
" random colorscheme
let themes = ['tokyonight','PaperColor', 'darkblue', 'delek', 'dracula', 'evening', 'gruvbox', 'industry', 'lunaperche', 'morning', 'pablo', 'quiet', 'ron', 'slate', 'sorbet', 'torte', 'zaibatsu', 'blue', 'default', 'desert', 'elflord', 'everforest', 'habamax', 'koehler', 'monokai', 'murphy', 'peachpuff', 'retrobox', 'shine', 'solarized', 'spaceduck', 'wildcharm', 'zellner']
let newcolor = 'colorscheme '.themes[localtime() % len(themes)]
execute newcolor
echom newcolor
endfunction
This is caused by redrawing, which Vim does a lot during normal operation and, since you are changing the colorscheme, a redraw is pretty much expected.
The problem is common to all :echo*
commands and thus explained only once, under :help :echo-redraw
, which is located just below :help :echo
and linked from :help :echom
with the following, rather explicit, wording:
See
:echo-redraw
to avoid the message disappearing when the screen is redrawn.
The fix consists in forcing a redraw before :echom
:
function Rcolor()
" random colorscheme
let themes = ['tokyonight','PaperColor', 'darkblue', 'delek', 'dracula', 'evening', 'gruvbox', 'industry', 'lunaperche', 'morning', 'pablo', 'quiet', 'ron', 'slate', 'sorbet', 'torte', 'zaibatsu', 'blue', 'default', 'desert', 'elflord', 'everforest', 'habamax', 'koehler', 'monokai', 'murphy', 'peachpuff', 'retrobox', 'shine', 'solarized', 'spaceduck', 'wildcharm', 'zellner']
let newcolor = 'colorscheme '.themes[localtime() % len(themes)]
execute newcolor
redraw
echom newcolor
endfunction