8

I wanted to highlight different indentation levels in vim, so I could identify large blocks of code more easily. I have some reasonable large nested for/while/with/try blocks and it gets hard to identify the block a am into, i.e. how many 'tabs' I have before the cursor.

Is there a way to highlight tabs?

This is what I have in mind:

try:
*   while True:
*   *   for foo in bar:
*   *   *   do()
*   if something:
*   *   done()
except bla:
*   exit()

Where * would be a special background color.

I would also settle for any other way to identify the indentation levels.

1
  • 1
    Mandatory snarky comment: perhaps you could try fixing your code instead of your editor. ;-) +1 though for an interesting question. I'm curious to see what this would actually look like. Commented Feb 6, 2012 at 17:06

2 Answers 2

8

The Indent Guides vim plug-in does exactly this kind of highlighting. I use it together with the listchars option (as Ackar pointed out).

Sign up to request clarification or add additional context in comments.

2 Comments

Oh wow, that plugin is pretty cool. It's way less obtrusive than I expected.
@Kristo I too use it and like it, unfortunately, it fails for blank likes since vim is incapable of highlighting blanks
3

You can use the listchars options to display specifics characters (see :help listchars).

For example if you want to show tabs you could use :

:set listchars=tab:*\     " Be careful : there is a space after the backslash
:set list

You can also change the highlighting colors using the highlight property for the SpecialKey group.

If you use vim in a terminal :

:highlight SpecialKey ctermfg=Cyan

See :help highlight for more informations.

You can also check :runtime syntax/colortest.vim to see all the available colors.

2 Comments

Maybe you should also mention how one can set the highlighting color for listchars.
Although this does exactly what I asked for, it does not work for me. I used the 'expandtab' option, so there are no 'tab' chars in my scripts, only spaces. I did not know about list[chars], great tip.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.