Vim HereDoc Backlight
I want to get syntax highlighting inside a bash heredoc. Also, I want bash to pivot variables and highlight command substitution in the heredoc.
Using these instructions as a starting point, I was able to add a dereference variable by looking at sh.vim . I'm guessing you could do command substitution in a similar way.
let s:bcs = b:current_syntax
unlet b:current_syntax
syntax include @YAML syntax/yaml.vim
syntax region hereDocYAML matchgroup=Statement start=/<<-\?\s*\z(YML\)/ end=/^\s*\z1/ contains=@YAML,hereDocDeref,hereDocDerefSimple
syn match hereDocDerefSimple "\$\%(\h\w*\|\d\)"
syn region hereDocDeref matchgroup=PreProc start="\${" end="}" contains=@shDerefList,shDerefVarArray
hi def link hereDocDeref PreProc
hi def link hereDocDerefSimple PreProc
My problem is that it does not work in any unit ( if
, function
, for
etc.). For example:
The only thing I know is that this is not an indentation issue. Changing the indentation has no effect, and using indentation outside the block works correctly.
source to share
contains
The on definitionshIf
appears to limit the possible highlights for any elements in the group shIfList
.
Adding your element to a group like this will give you region-specific highlighting:
syn cluster shIfList add=hereDocYAML
The same principle applies to all other blocks. For example:
syn cluster shFunctionList add=hereDocYAML
syn cluster shLoopList add=hereDocYAML
It should be noted that the approach you described is usually not suitable. First of all, it requires the included syntax file to use groups for its definitions contains
.
Also, it works for YAML because bash dereference syntax and YAML syntax do not conflict. For more complex syntaxes like sed or awk, you will have conflicts, and the rules for resolving those conflicts will most likely require a whole new syntax.
source to share