I want Vim to highlight tags (#tag) in my Markdown files.

I extended the default highlighting by creating ~/.vim/after/syntax/markdown.vim with the following content:

syn match mdTag '#\w\+'
hi link mdTag htmlTagName

This works as expected: tags are now highlighted as the htmlTagName style specifies. However, if the link appears in the end of a title, it is just highlighted as the title and not as a tag.

So Foo bar #tag works well, but # Chapter 2 #tag does not.

Is there a way to give the tag highlight rule a higher priority over the heading highlight rule?

1 Answer 1


Assuming you're using the default syntax rules for Markdown bundled with Vim, the reason why your mdTag rule is not matching inside a heading is that the existing heading rules (markdownH1, markdownH2, etc.) use a contains= attribute to specify which other rules can match inside the headings.

It turns out all those heading rules include a cluster @markdownInline in their contains=, which makes it easy to extend the list, since you can add to an existing cluster. You can add your mdTag to the list with:

syntax cluster markdownInline add=mdTag
  • 1
    You can also overrule the contains (effectively adding to it) with containedin
    – D. Ben Knoble
    Commented Aug 15, 2021 at 10:37

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.