2

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
1
  • 2
    That's the reason why I never use auto-indenters, even when writing in languages where indentation isn't vital.. I suggest keeping code clean while writing it and avoid any kind of automated manipulation. Commented Dec 14, 2011 at 2:03

2 Answers 2

4

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.

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

1 Comment

Fair enough, @MikeyB. That's pretty much what I suspected. (But there are enough packages, tutorials, etc. out there that I figured maybe the state of the art had advanced...)
2

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.

2 Comments

It's not the autoindent that's giving him trouble, it's the reindent command. But that's a handy link.
My apologies, now that I've taken the time to run the test provided by the OP I realise that a full reindent doesn't work. It looks as though something like this: svn.python.org/projects/doctools/trunk/utils/reindent.py could be easily turned into a python/vim plugin. That might be worth looking at instead.

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.