Saturday, February 17, 2007

Indent level highlighting

I have finally begun to delve into the world of VIM syntax highlighting. The syntax used by VIM that give all my different files such pretty colors have always been something of a mystery to me. Recently a problem caused me to make the jump into the :help syntax pages.

There was on my part some assumption that someone had already done what I wanted to do, which is to highlight each successive indent level in a file. This would be particularly helpful for python files, but other programming languages as well. The BlockHL looked like just what I was looking for. Interesting, and helpful, but not quite.

Delving into the documentation I eventually found after much scratching of the head how to do exactly what I wanted to. Piddling around with the highlight colors I came up with three variations. I think I like the right most one best.





















BlockHL syntax pluginProgressively darker indenting, kinda loud.


Alternate coloring. Better.Alternate color, with progressively darker shading. Best.

I've posted the very simple coloring script on vim, as a script plugin. See its page for further details.

*** update ***

Well well well, apparently some poeple actually read my postings sometimes and call me out on my shoddy programming skillz. Yes, its true. This plugin only works with TABs. Maybe if I get less lazy, I'll make it work with the tabstop/shiftwidth settings for you non tab users.

13 comments:

awt said...

Tried using your script. I saved it as ~/.vim/after/syntax/rb.vim, then opened a .rb file, but the indentation highlighting was a no-show.

Unknown said...

You need to save the file in ~/.vim/after/syntax/ruby for it to be recognized for ruby files. Or...probably you should have renamed the file to ~/.vim/after/syntax/ruby.vim.

Hope that helps!

awt said...

Ok. I discovered that I had accidentally created 'after' in my plugin dir, so I moved it to .vim/after/syntax/ruby.vim, and still no luck. It wouldn't be related to tabs vs. no tabs would it?

Unknown said...

Yes there _does_ appear to be something wrong. It worked for me great in vim/java/python syntaxes, but when I tried ruby nothing happened. I've removed the problematic code and it now works. There is an updated version on the vim script page (version 2).

elzr said...

This is fantastic! I had implemented the block highlighting myself but it never occurred to me to make it progressively darker! (Btw, for me, the one that doesn't alternate is the best--it's so 3d.) Thanks for posting this (with screenshots and all, real good...)!

I added one little modification to the the script, I added a \+ at the end of the last regexp (so it becomes:
syn match cTab7 /\(^\t\{6}\)\@<=\t\+/
). This way, the highlighting extends as far is it needs to.

One question though, I tried simplifying the script by substituting \@<= with \zs (:help says it's usually much faster) but it didn't work. Any idea why?

butlimous said...
This comment has been removed by a blog administrator.
Unknown said...

elzr,

Thanks for the comment, and the additional fix which I've added it into the script.

I did spend some time trying to use the \zs matcher, as I also saw the :help note about better performance. For some reason you can do:

/^\t\zs\t/

and

/\(^\t\)\@<=\t/

And they will match the exact same thing in VIM, but the \zs version isn't working with the syn match command.

I've asked on IRC and the answer I got was basically that b/c cTab1 region matches, the \zs won't start at the end of the first \t (basically \zs can't look back at previously matched content). But the \@<= can look back at other matched content, and so it works for this situation.

*shrug*

It sounds like voodoo to me.

Unknown said...

Does it possible to use code block level instead of Indent? I search this function, but can not find it.

Code block levels example:

{ // level 1 by gray1 color
{ // level 2 by gray2 color
}
}

Unknown said...

blockhl.vim is the answer to my question... ^_^

I want change the whole block's background color, not only the lead White space. I'll try to do it.

Thanks for your sharing.

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Nurul Imam said...

Thanks....