title: Vim HereDoc Highlighting
body: <p>I want to get syntax highlighting inside of a bash heredoc. In addition, I want bash variable dereference and command substitution highlighting in the heredoc.</p>
  
  <p>Using <a href="https://subvisual.co/blog/posts/87-smarter-heredoc-syntax-in-vim/" rel="noreferrer">these instructions</a> as a starting point, I was able to add variable dereferencing by examining <a href="https://github.com/vim/vim/blob/master/runtime/syntax/sh.vim" rel="noreferrer">sh.vim</a>. I imagine you could accomplish command substitution in a similar fashion.</p>
  
  <pre><code>let s:bcs = b:current_syntax
  
  unlet b:current_syntax
  syntax include @YAML syntax/yaml.vim
  
  syntax region hereDocYAML matchgroup=Statement start=/&lt;&lt;-\?\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
  </code></pre>
  
  <p>My problem is, this doesn't appear to work in any sort of block (<code>if</code>, <code>function</code>, <code>for</code>, etc.). For example:</p>
  
  <p><a href="https://i.stack.imgur.com/gU7K5l.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gU7K5l.png" alt="highlighting example"></a></p>
  
  <p>The only thing I know is that this is not an indentation problem. Changing indentation has no effect, and using indentation outside of a block works properly.</p>