I don't like the idea of turning text strings into commands when using a scripting language that ideally should be able to retain some semantics and structure for the kind of constructs, so I went about implementing a highlighting procedure able to use variables the following way instead:
function s:hl(group, attrs)
let l:command = "highlight" . " " . a:group
for name in keys(a:attrs)
let l:command .= " " . name . "=" . a:attrs[name]
endfor
execute l:command
endfunction
With above, I can pass a dictionary of highlight attributes for a group to set, where values of the dictionary may obviously be variable references.
Then one can get Vim to effectively end up running the same highlight <group> <name>=<value> ...
command, by calling the above function as follows, for example:
call s:hl("Keyword", { "guifg": "yellow" })
Since the function accepts a dictionary for its second parameter, other key-value pairs can be added accordingly, e.g. call s:hl("Normal", { "guifg": "white", "guibg": "black" })
.
With variables defined it'd be more like:
let s:chefchaouen_blue = "#468fea"
call s:hl("Comment", { "guifg": s:chefchaouen_blue })
Anyway, such approach may look to be more verbose -- and in the end is still turned into a command string to be used with the execute
command -- but I find Vim being able to highlight elements of its own script text -- e.g. the call s:hl(...)
statements above in a color scheme file -- worth it. Also, more of it may be checked for syntax errors earlier (than if concatenated into a string), although the difference may be negligible for most.