如何修复 Python 中 Vim 的长行断行行为?
这就是我的问题。假设我有一个 Python 文件,并且正在输入很长的一行,就像这里的最后一行:
class SomeClass(object):
def some_method(self):
some_variable = SomeOtherClass.some_other_method(some_parameter=some_value)
当我在 Vim 中输入此内容时,会发生这种情况:
class SomeClass(object):
def some_method(self):
some_variable =
SomeOtherClass.some_other_method(some_parameter=some_value)
这不仅是糟糕的风格,而且破坏了 PEP8。我希望发生的是:
class SomeClass(object):
def some_method(self):
some_variable = SomeOtherClass.some_other_method(
some_parameter=some_value)
这与 PEP8 保持一致。 (出于本次讨论的目的,我只对换行行为感兴趣,而不是缩进行为。)
编辑: breakat
仅与 结合使用linebreak
控制行的显示方式。它(显然)不能与 textwidth
结合使用来确定插入硬换行符的位置。所以我下面的想法行不通......
令人惊讶的是,我没有发现任何迹象表明其他人也有这个问题,这让我认为我做错了什么。尽管如此,我的想法是将 (
字符添加到 breakat
设置中(以及 [
和 {
,而我 我已经尝试过了
;这是 :set breakat
的输出:
breakat= ^I!@*-+;:,./?([{
但是,无论我做什么,Vim 都坚持在上面的“=”之后中断。我对长函数名也有同样的问题,其中它会在 def
之后立即中断。
这是我的 .vimrc 的完整内容:(
set nobackup
set nowritebackup
set noswapfile
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smarttab
set smartindent
set textwidth=80
set wrap
set breakat=\ ^I!@*-+;:,./?\(\[\{
filetype indent on
filetype on
filetype plugin on
我没有安装任何插件等来试图解决这个问题。)
有谁知道怎么做吗?我可以让 Vim 遵守我的 Breakat 设置,或者有其他关于处理这种行为的最佳方法的想法吗?
So here's my problems. Let's say I have a Python file and I'm typing a really long line, like the last one here:
class SomeClass(object):
def some_method(self):
some_variable = SomeOtherClass.some_other_method(some_parameter=some_value)
When I type this in Vim, this happens:
class SomeClass(object):
def some_method(self):
some_variable =
SomeOtherClass.some_other_method(some_parameter=some_value)
That's not just bad style, it breaks PEP8. What I'd like to happen is:
class SomeClass(object):
def some_method(self):
some_variable = SomeOtherClass.some_other_method(
some_parameter=some_value)
Which is in keeping with PEP8. (For the purposes of this discussion, I'm only interested in the line breaking behavior, not the indentation behavior.)
Edit: breakat
only works in conjunction with linebreak
to govern how lines are displayed. It does not (apparently) work in conjunction with textwidth
to determine where hard line breaks are inserted. So my idea below will not work...
Surprisingly, I have found nothing out there indicating others share this problem, which leads me to think I'm doing something wrong. Nevertheless, my idea was to add the (
character to the breakat
setting (along with [
and {
while I was at it).
I've tried this; here's the output of :set breakat
:
breakat= ^I!@*-+;:,./?([{
However, it's to no avail. No matter what I do, Vim insists on breaking after the "=" above. I have this same problem with long function names as well, where it'll break right after def
.
Here are the complete contents of my .vimrc:
set nobackup
set nowritebackup
set noswapfile
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smarttab
set smartindent
set textwidth=80
set wrap
set breakat=\ ^I!@*-+;:,./?\(\[\{
filetype indent on
filetype on
filetype plugin on
(I have no plugins etc. installed for the purpose of trying to figure this out.)
Does anyone have any idea how I can get Vim to obey my breakat setting, or any other thoughts about the best way to deal with this behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有设置换行符。没有它,vim 会忽略breakat 变量。请参阅此处了解详细信息以及许多 vim 优点。另请注意,您需要设置 nolist,因为它会破坏换行符。
另一篇关于自动换行的博客文章此处。这篇文章指出,某些文件类型不会自动包含格式选项“t”标志。没有它,就不会发生自动换行。
我认为最简单的方法是添加一个映射来运行
:%! pythonTidy。 pythonTidy 是一个 Python 脚本,用于在 stdIn 上获取 python 代码并将漂亮的版本输出到 stdOut。通过该映射,它将允许您在当前文件上运行它,并用重新格式化的版本替换整个内容。添加一个自动命令,以便在每次退出 python 文件中的插入模式时运行它,并且应该进行设置。
You aren't setting linebreak. Without it, vim ignores the breakat variable. See here for details, and lots of vim goodness. Also note that you need to set nolist as it breaks linebreak.
Another blog post dealing with word wrapping here. This one notes that some filetypes do not auto include the format options "t" flag. Without it, auto wrapping does not occur.
I think the simplest thing to do would be to add in a mapping to run
:%! pythonTidy
. pythonTidy is a script, in python, to take python code on stdIn and output a pretty version to stdOut. With that mapping, it will allow you to run it on your current file, and replace the entire contents with the reformatted version. Add an autocommand to run this whenever you exit insert mode in python files, and you should be set.