Vim python缩进改变了代码语义

时间:2011-12-14 01:57:21

标签: python vim indentation auto-indent

我正在尝试学习vim :-)在自动缩进中改变我的python代码的含义时遇到了一些麻烦。有没有什么办法可以获得autoindent的优点而不会遇到类似下面的行为?

我从一个python文件开始:

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

print 'Always print this line.'

使用vim pytest.pygg=G:wq

进行自动缩进

噢,我弄乱了我的剧本:

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

    print 'Always print this line.'

试图保持这真的香草:

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

2 个答案:

答案 0 :(得分:4)

是的,不要这样做。

VIM中autoindent的全部意义在于让编辑根据语言格式规则猜测空白应该去哪里。但是:

enter image description here

如你所知,Python空白非常重要。正如医生所说:Don't Do That Then.

答案 1 :(得分:2)

在vim中进行自动缩进只是让你的tabstop,shiftwidth,softtabstop和expandtab设置与你正在编辑的文件一致的问题。我用这个插件:

http://freecode.com/projects/vindect

从你正在编辑的python文件中运行这些设置,并确保即使你碰巧编辑了另一个人文件设置他们的空白区域与你不同,vim也能正常运行。

编辑:如果你想完全重写一个python源文件(替换gg = G),你需要一个像这样的python特定的reindenter:http://svn.python.org/projects/doctools/trunk/utils/reindent.py你很容易变成一个vim命令。