How can I fix Vim's line breaking behavior for long lines in Python?

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?


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 . 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.

链接地址: http://www.djcxy.com/p/9556.html

上一篇: django升级后的ContentNotRenderedError

下一篇: 如何修复Vim在Python中对长行的行分行为?