Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting python code blocks in vim

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.

like image 308
Rafael Barbosa Avatar asked Feb 06 '12 16:02

Rafael Barbosa


People also ask

How do I enable Python syntax highlighting in Vim?

Add the text, “syntax on” anywhere in the file to enable syntax highlighting permanently for vim editor. Save and close the file by typing ':x'. For disabling the feature, just re-open . vimrc file, change the text “syntax on” to “syntax off” and save the file.


2 Answers

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

like image 192
Walter Avatar answered Sep 25 '22 01:09

Walter


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.

like image 33
Sylvain Cleymans Avatar answered Sep 26 '22 01:09

Sylvain Cleymans