Frage

I'm trying to learn vim :-) Having some real trouble with auto-indent changing the meaning of my python code. Is there any way to get the goodness of autoindent without running into behaviors like the one below?

I start with a python file:

me@host:/tmp$ cat pytest.py 
if False:
    print('Never',
          'print this line')

print 'Always print this line.'

Auto indent with vim pytest.py, gg=G, :wq

D'oh, I messed up my script:

me@host:/tmp$ cat pytest.py 
if False:
    print('Never',
            'print this line')

    print 'Always print this line.'

Trying to keep this really vanilla:

me@host:/tmp$ cat ~/.vimrc 
filetype plugin on
filetype indent on
autocmd FileType python set sw=4
autocmd FileType python set ts=4
autocmd FileType python set sts=4
autocmd FileType python set textwidth=79
War es hilfreich?

Lösung

Making auto indent work in vim is just a question of having your tabstop, shiftwidth, softtabstop and expandtab settings consistent with the file you're editing. I use this plugin:

http://freecode.com/projects/vindect

Which works these settings out from the python file you're editing and makes sure that vim behaves correctly even if you happen to edit another persons file who sets up their white space differently from you.

EDIT: If you wish to do a full reindent of a python source file (to replace gg=G), you'll need a python specific reindenter like this one: http://svn.python.org/projects/doctools/trunk/utils/reindent.py which you could easily turn into a vim command.

Andere Tipps

Yeah, don't do that.

The whole point of autoindent in VIM is to have the editor guess at where the whitespace ought to go according to language formatting rules. However:

enter image description here

Python whitespace is important, as you know. As the doctor would say: Don't Do That Then.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top