有谁知道一种方法让 vim 换行长行文本,使得换行文本的位置基于当前行的缩进? 我不想重新格式化我的代码,只是为了让它显示得漂亮。
例如,如果我设置我的设置,以便行:
print 'ProcessorError(%r, %r, %r)' % (self.file, self.index, self.message)
在包装为:时显示,
print 'ProcessorError(%r, %r, %r)' % (self.file, self.index,
self.message)
那么如果我编写这样的代码块:
def __repr__(self):
return 'ProcessorError(%r, %r, %r)' % (self.file, self.index, self.message)
它会包装为这样的内容:
def __repr__(self):
return 'ProcessorError(%r, %r, %r)' % (self.file, self.index,
self.message)
我希望它显示为:
def __repr__(self):
return 'ProcessorError(%r, %r, %r)' % (self.file, self.index,
self.message)
编辑:阅读 Don Werve 的回复后,似乎我确实在寻找 breakindent
选项,但该选项仍在“等待更新的补丁”列表中(请参阅Vim TODO)。 所以我想知道让 vim 使用 breakindent
的最简单的方法是什么? (我不在乎我必须使用什么版本的 vim。)
Does anyone know of a way to get vim to wrap long lines of text such that the position of the wrapped text is based on the indentation of the current line? I don't want to reformat my code, just for it to be displayed prettily.
For instance, if I set my settings so that the line:
print 'ProcessorError(%r, %r, %r)' % (self.file, self.index, self.message)
is displayed when wrapped as:
print 'ProcessorError(%r, %r, %r)' % (self.file, self.index,
self.message)
then if I write a block of code like this:
def __repr__(self):
return 'ProcessorError(%r, %r, %r)' % (self.file, self.index, self.message)
it wraps to something like this:
def __repr__(self):
return 'ProcessorError(%r, %r, %r)' % (self.file, self.index,
self.message)
I would prefer for it to be displayed as:
def __repr__(self):
return 'ProcessorError(%r, %r, %r)' % (self.file, self.index,
self.message)
Edit: after reading Don Werve's response, it seems that I am indeed looking for the breakindent
option, but the option is still on the "Awaiting updated patches" list (see Vim TODO). So what I'd like to know is what is the easiest way to get vim working with breakindent
? (I don't care what version of vim I have to use.)
发布评论
评论(5)
我在 SuperUser 上问了同样的问题,最终找到了这个问题,找到了补丁,并且更新了补丁以与 Fedora 11 中的 Vim 7.2.148 配合使用。
您可以使用 yumdownloader --source vim 获取源 RPM。 然后将
Patch3312:
行和%patch3012 -p1
行添加到规范文件中,并构建 rpm。I asked the same question on SuperUser, eventually found this question, found the patch, and updated the patch to work with Vim 7.2.148 from Fedora 11.
You can use
yumdownloader --source vim
to get the source RPM. Then add aPatch3312:
line and a%patch3012 -p1
line to the spec file, and build the rpm.您正在寻找
breakindent
您可能还需要参考 此帖子。
You're looking for
breakindent
You may want to also refer to this thread.
我推荐这个vim脚本:
http://www.vim.org/scripts/script .php?script_id=974
“这个 python 缩进脚本尝试更接近 PEP 8 中建议的内容 (http://www.python.org/peps/pep-0008.html)。特别是,它处理由 open(括号)、[brackets] 和 { 隐含的连续行正确地使用大括号},并且它以不同的方式缩进多行 if/for/while 语句。”
I recommend this vimscript:
http://www.vim.org/scripts/script.php?script_id=974
"This indentation script for python tries to match more closely what is suggested in PEP 8 (http://www.python.org/peps/pep-0008.html). In particular, it handles continuation lines implied by open (parentheses), [brackets] and {braces} correctly and it indents multiline if/for/while statements differently."
要控制 Python 代码的缩进,请参阅
:h ft-python-indent
。 例如,如果您在有未闭合括号的情况下执行换行,这将使 Vim 缩进两倍于shiftwidth
:但是
&sw * 2
是默认值,所以不确定为什么它不适合你。 它适用于手动换行符或textwidth
诱导的换行符。上述设置需要在
.vimrc
中,或者需要在 Vim 进入 Python 模式之前以某种方式设置。 请确保:setf python
或者您处于 Python 模式。For controlling the indentation of Python code, see
:h ft-python-indent
. This for example will make Vim indent two times theshiftwidth
if you do a newline while there's an unclosed paren:However
&sw * 2
is the default, so not sure why it's not working for you. It works for me with manual newlines or withtextwidth
-induced newlines.The above setting needs to be in
.vimrc
or needs to be set somehow before Vim enters Python mode. Be sure to:setf python
or that you're otherwise in Python mode.我认为设置 textwidth=80 应该可以。
I think set textwidth=80 should do it.