Search code examples
vimvim-plugin

How do I expand a variable as an argument to a highlight command?


In the context of a Vim plugin, I would like to add the ability to customise highlight colours.

How do I expand the variable in a scenario like the following:

    get s:cursorline_bg = get(g:, 'customcolours_cursorline_bg', 'black')
    hi CursorLine cterm=NONE ctermbg=s:cursorline_bg

As it stands this gives an error as the variable is being literally interpreted as the word 's:cursorline_bg' and not the value 'black'.

Any help would be greatly appreciated!


Solution

  • You need to construct a string and interpret it using execute:

    exe 'hi CursorLine cterm=NONE ctermbg=' . s:cursorline_bg
    

    Dot (.) is string concatenation in vim.