tags:

views:

152

answers:

5

I'm looking for a way in vim to easily visualize the various indent levels of python code. It would help if there was always a vertical rule at the beginning of the current line. That way I can scan down the code to see where the current block ends. Are there any plugins out there that do this?

A: 

The first thing that comes to mind is that you could benefit from a plugin that implements code folding.

Here is a tutorial with examples (scroll down to "Code folding") that recommends the use of the "Efficient python folding" plugin for vim.

screenshot

jathanism
A: 

in vim (no plugins needed):

:set list

will display tabs as '^I' and EOL as '$' by default.

with

:set lcs=tab:>>

you'd set '^I' to '>' (see more on that by :help listchars).

i'm not sure, but there should be another option to set the tab width.

also you may set

:set autoindent

for python

mathume
It is recommended to indent Python with 4 spaces, not with tabs.
ZyX
@abhijit: you can accomplish what ZyX mentioned by::set shiftwidth=4:set autoindentuse CTRL+T in insert mode
mathume
@mathume This was about the fact that most of Python code is indented with spaces, so setting `list` and `lcs` will not show anything. And `expandtab` is maybe better for python.
ZyX
A: 

I think the command you're looking for is "colorcolumn", it's new to vim 7.2 or 7.3 I think.

You might be able to work something up with the autocommand trigger CursorMoved

autocmd CursorMovedI * set colorcolumn=match(getline("."),"\S")

You will probably have to play with this, using intermediate variables and such.

What this would do (if properly buried inside a function), is put a single vertical line at the starting character of the current line. This might be handy, but should probably only be put on a toggle.

EDIT: This turns out to be a bit more complicated than I thought originally. Basically you have to eliminate the effect of literal tabs (if they show up in your file)

autocmd CursorMoved * let &colorcolumn=matchend(substitute(getline("."),'\t',repeat(" ",&ts),'g'),"\\S")

When I was first putting this together I sortof thought it was silly, but just playing around with it for a few minutes, I sortof like the effect.

Note that you may or may not want a CursorMovedI version.

jkerian
ZyX
You are quite correct...
jkerian
A: 

You can define you own syntax items for it (or use matches). Quick and dirty solution:

let colors=["red", "white", "yellow", "green", "blue"]
let matchids=[]
for level in range(1, len(colors))
    execute "hi IndentLevel".level." ctermbg=".colors[level-1]." guibg=".colors[level-1]
    call add(matchids, matchadd('IndentLevel'.level, '^   '.repeat('    ', level-1).'\zs '))
endfor

This will highlight five first indentation levels with different colors.

To disable:

while !empty(matchids)
    call matchdelete(remove(matchids, 0))
endwhile
ZyX
+4  A: 

You could simply emulate indentation guides. It's simpler and more effective, in my opinion. Please, take a look at my answer to the question about indentation guides.

ib