Home > Net >  how can I change the colorscheme programmatically with vimscript?
how can I change the colorscheme programmatically with vimscript?

Time:11-21

I want to change the colorscheme inside a vimscript like:

let g:scheme = "default"
colorscheme scheme

and i get the following error:

colorscheme "scheme" cannot be found

why is the variable 'scheme' not linked to the string 'default' when i execute the :colorscheme command ?

CodePudding user response:

A "vimscript" or "viml script" or whatever (naming is hard) is really just a sequence of Ex commands, sometimes called "colon commands".

Most of those commands, like :colorscheme, only take straight up strings as input and don't even try to evaluate variables/expressions:

colorscheme scheme

In your example, scheme is treated as a string, not as a variable so, of course, you get an error because you don't have a colorscheme called scheme.

In those cases, which are relatively common, variables must be evaluated before being passed to the Ex command. This is done with :help :execute and the :help expr-.. operator:

execute 'colorscheme ' .. scheme
  • Related