I would like to highlight all the HereDocs in a particular perl file as perl code. For example, I'd like to apply the normal perl highlighting to the subroutine in this HereDoc:

my $code = <<'CODE';
   sub test {
      say "this is a test";

I thought I could do this with something like:

syn region perlHereDocPL    start=+<<\s*'\z([^\\']*\%(\\.[^\\']*\)*\)'+ matchgroup=perlSnip end=+^\z1$+ contains=@perlTop

But apparently this doesn't work like I thought it did. Thanks!

  • 1
    Does this file already have some other syntax highlighting applied to it? Commented Mar 13, 2016 at 20:05
  • Yes, it is a perl test file. Perl syntax highlighted. Commented Mar 14, 2016 at 2:19
  • Right, so you want to highlight Perl code inside Perl code [insert snarky comment about Perl readability] :p Commented Mar 14, 2016 at 2:20
  • Typically the heredoc would be formatted as a string. For this particular file, I want to format it as perl. I've seen similar configurations, i.e. javascript inside an <script> tag in an HTML file. Commented Mar 14, 2016 at 15:28

1 Answer 1


It looks like I found a solution. I was able to adapt config that formatted heredoc as SQL:

" .vim/after/syntax/perl/heredoc-perl.vim
runtime! syntax/perl.vim
unlet b:current_syntax
syntax include @Perl syntax/perl.vim

syntax region sqlSnip matchgroup=Snip start=+<<['"]RAW['"].*;\s*$+ end=+^\s*RAW$+ contains=@Perl
syntax region sqlSnip matchgroup=Snip start=+<<['"]TIDIED['"].*;\s*$+ end=+^\s*TIDIED$+ contains=@Perl

This only applies to heredocs that are identified by RAW or TIDIED, but that is perfect for my purposes.

  • 1
    I think you can lose the first two lines here (runtime! syntax/sql.vim, unlet b:current_syntax)? As you're not doing anything with the SQL syntax... Commented Mar 14, 2016 at 23:25
  • Thanks. That should have been runtime! syntax/perl.vim instead of runtime! syntax/sql.vim. That probably isn't necessary either since the rest of the file is using the perl syntax. Commented Mar 15, 2016 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.