I would like the html tag attribute id to be highlighted differently from the rest of the tag attributes.

Like the Atom editor:

enter image description here

Given this line from the html5.vim plugin

syn keyword htmlArg contained id

I have tried this:

syn match htmlArgId /id/ containedin=ALL
hi htmlArgId guifg=blue

but it doesn't work it only highlights id outside of html tags.


Well you can of course dive into the complex syntax rules, see @Carpetsmoker's answer. However that might not always work correctly, depending on the order of the syntax rules and their priorities. Therefore, for such simple things it is usually better to use matchadd() function for highlighting special items in addition to the syntax rule.

First define your highlighting group you want to use:

hi htmlTagID ctermbg=blue guifg=blue
call matchadd('htmlTagID', '\<id\>\ze=')

This could be put into a filetype plugin, like ~/.vim/after/ftplugin/html.vim (create non-existing directories). For a more complete solution, you would need to store the return value of matchadd, to be able to clear the match later with matchdelete().

Note, matching is window-local so if you have the same buffer open in several windows, you might not see the highlighting in every window. For that you would also need to call the highlighting function on a WinEnter autocommand (or similar).

  • This is pretty close to what I want. I changed it up to use an autocmd in my vimrc. autocmd BufWinEnter *.html call matchadd('htmlArgId', '\<id\>\ze=') Do you see any problem with this?
    – mhinton
    Jun 23 '17 at 16:40

You'll need to modify the contains of syn region htmlTag to include your new htmlArgId. The full line would be:

syn region htmlTag start=+<[^/]+ end=+>+ fold contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent,htmlCssDefinition,@htmlPreproc,@htmlArgCluster,htmlArgId

I just tacked on ,htmlArgId at the end; the rest is from /usr/share/vim/vim80/syntax/html.vim.

After that you can just use:

syn keyword htmlArgId contained id
hi htmlArgId ctermfg=blue guifg=blue

You can add this in your vimrc with an autocmd (autocmd Syntax html ...), or you can put it in ~/.vim/after/syntax/html.vim. See How can I add additional syntax highlighting rules in my local vimrc? for details on that.

  • This doesn't work for me even though the output of :syn shows the htmlTag region value containing htmlArgId.
    – mhinton
    Jun 23 '17 at 16:37
  • How did you apply the commands @MHinton? It worked for me when I tested it yesterday by just issuing the commands from Vim (:syn region .... etc). It might also be version issue? I'm using version 8.0.628 Jun 23 '17 at 16:39
  • Issuing the commands doesn't work for me. I am using 8.0.648 of macvim and 8.0.662 of vim.
    – mhinton
    Jun 23 '17 at 16:45
  • @MHinton Curious, I just tried it again by copy/pasting from the answer and it works for my in both terminal Vim and Gvim. I don't have a mac so I can't test it there. Are you sure the filetype is actually html and not one of the deviates like htmldjango? I also tested with just a blank file with only <img id="catcher" src="#"> (as in your example). Jun 23 '17 at 16:50

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.