Vim python 缩进改变代码语义
我正在尝试学习 vim :-) 在自动缩进改变我的 python 代码的含义方面遇到了一些真正的麻烦。有没有什么方法可以发挥自动缩进的优点,而不会遇到下面这样的行为?
我从一个 python 文件开始:
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,我搞砸了脚本:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,不要这样做。
VIM 中自动缩进的全部目的是让编辑器根据语言格式规则猜测空白应该去哪里。但是:
如您所知,Python 空格很重要。正如医生所说: 不要那么就这样做。
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:
Python whitespace is important, as you know. As the doctor would say: Don't Do That Then.
让自动缩进在 vim 中工作只是让你的 tabstop、shiftwidth、softtabstop 和 Expandtab 设置与你正在编辑的文件一致的问题。我使用这个插件:
http://freecode.com/projects/vindect
它可以从你正在编辑的 python 文件,并确保 vim 行为正确,即使你碰巧编辑的另一个人的文件设置的空白与你不同。
编辑:如果您希望对 python 源文件进行完全重新缩进(以替换 gg=G),您将需要一个特定于 python 的重新缩进器,如下所示: http://svn.python.org/projects/doctools/trunk/utils/reindent.py 您可以轻松地转成vim命令。
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.