Proper Html Attribute Highlighting In Vim?
Solution 1:
Exploration
Here is a very useful function I've found somewhere (a long time ago, probably on the Vim Wiki) that gives you the syntax group of the word/symbol under your cursor:
function! SynStack() if !exists("*synstack") returnendifechomap(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")') endfunc
Just place your cursor on the item you want to inspect and type
:call SynStack()
to echo the syntax group in the command-line.If I place my cursor on the
=
in<div id="example"></div>
, the output ofSynStack()
is['htmlTag']
.With the cursor on
<>
I get['htmlTag']
as well.With the cursor on
div
I get['htmlTag', 'htmlTagN', 'htmlTagName']
which means that the color ofdiv
(h1
,p
…) is defined via a special syntax group calledhtmlTagName
that inherits fromhtmlTag
.Some alternative/custom syntax files may define other syntax groups with slightly varying name so my example is only valid for me. You'll have to play with
SynStack()
to get the correct syntax groups.Reflexion
With the info we have gathered so far, it's obvious that the tag name (
['htmlTagName']
) can be styled independtly from the rest of the tag but it doesn't seem doable to highlight the=
differently. Because it is part of the same syntax group as<>
, the=
will necessarilly be highlighted the same.We have 2 possibilities:
a.
<
,=
and>
are the same colour whilediv
is different.b.
<
,div
,=
and>
are all the same colour.The original theme followed path
a
which I didn't like, so I had to customize it a little (pathb
) with the few lines in my answer to the previous question:hi htmlTag guifg=#90b0d1 gui=NONE hi htmlSpecialTagName guifg=#90b0d1 gui=NONE hi htmlTagName guifg=#90b0d1 gui=NONE hi htmlEndTag guifg=#90b0d1 gui=NONE
As it is, having the
=
coloured differently than<>
is not possible. If we want to colorize the=
we are going to edit the HTML syntax file and your colorscheme, cowboy style.Action
The first step is to make a local copy of the default HTMl syntax file:
$ cp /usr/share/vim/vim73/syntax/html.vim ~/.vim/syntax/html.vim
The next step is to edit this file. We are going to perform two changes:
add the definition of the
htmlEqualSign
syntax groupLine 44 should be (Attention! Not thoroughly tested.):
syn match htmlEqualSign contained "="
add
htmlEqualSign
to thehtmlTag
groupLine 40 of
~/.vim/syntax/html.vim
should be changed from:syn region htmlTag start=+<[^/]+end=+>+contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent,htmlCssDefinition,@htmlPreproc,@htmlArgCluster
to:
syn region htmlTag start=+<[^/]+end=+>+contains=htmlTagN,htmlString,htmlArg,htmlValue,htmlTagError,htmlEvent,htmlCssDefinition,@htmlPreproc,@htmlArgCluster,htmlEqualSign
The last step is to edit your colorscheme so that it colorizes
=
the way you want. You do that by adding this line somewhere in your colorscheme:hi htmlEqualSign guifg=#00ff00
With the color of your choice, of course.
But I think that you want
=
to be the same color asid
(that's not very clear from your question). For that, we are going to "link" thehtmlEqualSign
group to the one being used for attributes. Again,:call SynStack()
is of great help: the syntax group for attributes ishtmlArg
so the line to add to your colorscheme would be:hi link htmlEqualSign htmlArg
Post a Comment for "Proper Html Attribute Highlighting In Vim?"