8

Possible Duplicate:
Why is vim drawing underlines on the place of tabs and how to avoid this?

When indenting PHP code in VIM 7.0 on CentOS 5.x, HTML links are shown underlined. This is very handy, but in some places I have indented PHP code in that HTML, and the whole indentation is underlined:

            <li class="picture">
________________<a href="<?=$linkUrl?>">
____________________<img src="/<?=$img['source']?>" alt="Picture"/>
____________________<? if ($someCondition): ?><span class="info"><?=$img['info']?></span><? endif; ?>
________________</a>
            </li>

Is there any way to tell the syntax highlighter to ignore line-leading whitespace in HTML links?

Community
  • 1
  • 1
dotancohen
  • 30,064
  • 36
  • 138
  • 197

2 Answers2

10

I managed to achieve this through modifying $VIMRUNTIME/syntax/html.vim. Make a copy to ~/.vim/syntax/html.vim (.vim is named vimfiles on Windows), and replace the original syntax definition

syn region htmlLink start="<a\>\_[^>]*\<href\>" end="</a>"me=e-4 contains=@Spell,htmlTag,htmlEndTag,htmlSpecialChar,htmlPreProc,htmlComment,javaScript,@htmlPreproc

with the following:

syn region htmlLink start="<a\>\_[^>]*\<href\>" end="</a>"me=e-4 keepend contains=@Spell,htmlTag,htmlEndTag,htmlSpecialChar,htmlPreProc,htmlComment,htmlLinkText,javaScript,@htmlPreproc
syn match htmlLinkText contained contains=@Spell,htmlTag,htmlEndTag,htmlSpecialChar,htmlPreProc,htmlComment,htmlLinkText,javaScript,@htmlPreproc "^\s*\zs.\{-}\ze\s*$"
syn match htmlLinkText contained contains=@Spell,htmlTag,htmlEndTag,htmlSpecialChar,htmlPreProc,htmlComment,htmlLinkText,javaScript,@htmlPreproc "\S.\{-}\ze\s*$"

Further down, change

HtmlHiLink htmlLink                    Underlined

to

HtmlHiLink htmlLinkText                Underlined

Voila! Basically, this introduces another contained syntax group htmlLinkText, which does not match leading and trailing whitespace, and applies the highlighting to that instead.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Thank you. For some reason, now none of the links are highlighted but H2 and P tags are highlighted! I went over the changes carefully, I can't seem to find them. The edits are around lines 150 and 253 [if you would like to take a look](http://pastebin.com/kDPAH7g7). Thank you Ingo! – dotancohen Apr 18 '12 at 03:43
  • Your edits are fine, and your file works for me. Do you have any othe definitions, maybe in an ~/.vim/after/syntax/ file? – Ingo Karkat Apr 18 '12 at 06:46
  • No, no other files or definitions, I even tried with a blank ~/.vimrc (so no other system-wide .vimrc would get in the way). This is in a Cygwin terminal SSHing into a CentOS 5.x box, VIM 7.0. – dotancohen Apr 18 '12 at 16:37
  • 1
    @dotancohen: I think I've now encounted what you referred to as "H2 and P tags are highlighted now". This can be fixed by adding "keepend" to the htmlLink definition; I've updated my answer. – Ingo Karkat Apr 26 '12 at 13:02
  • Ingo, thank you, that does exactly the trick! This will reduce a real eyesore for me, thanks. I took a look at your site, and I though that you might like to know that Hannah means "beautiful" in Hebrew too. – dotancohen Apr 28 '12 at 20:14
  • I added a bounty for your diligence, but I can only award it in another 24 hours. – dotancohen Apr 28 '12 at 20:15
  • @dotancohen: I appreciate this; glad that it's now working for you, too! I will send this patch to the maintainer of syntax/html.vim, so that maybe this gets into the main distribution. – Ingo Karkat Apr 29 '12 at 09:07
8

You can do this:

:hi link htmlLink NONE
kev
  • 155,172
  • 47
  • 273
  • 272
  • Thanks, but that disables all link highlights, which I happen to find useful. I am looking to disable the highlight only on line-leading whitespace. – dotancohen Apr 14 '12 at 16:12