4

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?

ib.
  • 27,830
  • 11
  • 80
  • 100
Abhijit Rao
  • 1,316
  • 1
  • 9
  • 13

5 Answers5

5

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.

Community
  • 1
  • 1
ib.
  • 27,830
  • 11
  • 80
  • 100
1

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
(source: dancingpenguinsoflight.com)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
jathanism
  • 33,067
  • 9
  • 68
  • 86
0

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
  • 57
  • 1
  • 3
  • It is recommended to indent Python with 4 spaces, not with tabs. – ZyX Aug 27 '10 at 21:56
  • @abhijit: you can accomplish what ZyX mentioned by: :set shiftwidth=4 :set autoindent use CTRL+T in insert mode – mathume Aug 27 '10 at 22:12
  • 1
    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 Aug 27 '10 at 22:20
0

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
  • 16,497
  • 3
  • 46
  • 59
  • 2
    You have at least two errors: 1. replace `set colorcolumn` with `let &colorcolumn`: `set` command does not evaluate expressions. 2. Escape backslash in the second argument of match() (or replace double quotes with single). – ZyX Aug 27 '10 at 22:17
0

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
  • 52,536
  • 7
  • 114
  • 135