I use vim and type a lot of Django html code and I don't like the htmldjango.vim or html.vim highlighting syntax. I decided I should just create my own basic syntax highlighting for Django html. All I want is for all html tags (ul, li, html, head, title, body, p etc.) to be coloured blue, I want all variables (everything which starts and ends with {{ }} or {% %}) to be coloured in yellow, and all links and link names (a href='link'>linkNames) to be coloured in red.

So my question is, how do I tell vim that if the code is either of the tags below:

<html> </html> <body> </body> <ul> </ul>

etc., then to colour it blue?

and if the code starts with

{{ or {%

and ends with

}} or %}

then to colour it yellow? And if it is a link or a link name, then to colour it red?

  • Syntax scripts deal with defining what is a tag or attribute or whatever and colorschemes deal with how to display those symbols. What is your issue with html? How tags and stuff are defined or how they are displayed? – romainl Jan 11 '14 at 20:20
  • @romainl how they are display. The tags are defined fine. – user216485 Jan 11 '14 at 20:30
  • Then you don't need to write a syntax file or modify an existing one: modify your colorscheme. – romainl Jan 12 '14 at 7:05

Start with the chapter about writing your own syntax plugin :help usr_44.txt in the documentation. With your simple requirements, it's probably enough to pick some relevant parts from $VIMRUNTIME/syntax/html.vim. You can then look up any particular topics via :help :syn-...

Be aware that a lot of effort and tweaking has already been spent on the default syntax plugin; I use it myself and am quite satisfied with it. If you just find it too "colorful", i.e. that it highlights too many different parts, you can just keep the syntax and redefine some syntax groups to use the same highlight group, e.g.

:hi link htmlTag Normal
:hi link htmlTitle Normal

First you set up a series of regular expressions to define regions of text for highlight, as detailed in :help syntax. Link those highlight groups to one of the common groups as detailed in :help group-name. Then your colorscheme should set colors for that group using a :highlight command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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