Search code examples
bashvimvim-syntax-highlighting

Highlight bash internal varibles using VIM


Is it possible to Highlight bash internal varibles using VIM?

For example the variables described on this page would appear a different colour than the user defined variables.

http://tldp.org/LDP/abs/html/internalvariables.html


Solution

  • You can define additional syntax keywords for those built-in variables. Put the following into ~/.vim/after/syntax/sh.vim:

    syntax keyword shBuiltInVariable BASH BASH_ENV BASH_VERSION containedin=shDerefSimple
    highlight def link shBuiltInVariable Special
    

    The containedin= is necessary because shell variables are already parsed by existing syntax groups, and these additional overrides need to go in there to match.

    Also note that $VIMRUNTIME/syntax/sh.vim supports multiple shell dialects; if you use different shells, you need to add proper conditionals (b:is_bash etc.) around your additions.