Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

echom not displaying after changing colorscheme in Vim

Tags:

vim

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
like image 360
Dread Avatar asked Dec 19 '25 15:12

Dread


1 Answers

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

Rcolor()

like image 76
romainl Avatar answered Dec 21 '25 08:12

romainl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!