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

So here are my problems. Say I have a Python file and I type a very long line, for example, the last:

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) 

This is not just bad style, it breaks down PEP8. I would like to:

 class SomeClass(object): def some_method(self): some_variable = SomeOtherClass.some_other_method( some_parameter=some_value) 

This corresponds to PEP8 . (For the purpose of this discussion, I'm interested in the behavior of line breaking, not the indentation behavior.)

Edit: breakat only works with linebreak to control the display of lines. It does not work (apparently) in conjunction with textwidth to determine where hard line breaks are inserted. So my idea below will not work ...

Surprisingly, I did not find anything, indicating that others share this problem, which makes me think that I am doing something wrong. However, my idea was to add a character ( to the breakat setting (along with [ and { when I was on it).

I have tried this; here's the output :set breakat :

 breakat= ^ I!@ *-+;:,./?([{ 

However, this is unsuccessful. No matter what I do, Vim insists on breaking after the "=" above. I have the same problem with long function names where it will break right after def .

Here is the full 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 do not have plugins, etc. installed to figure this out.)

Does anyone know how I can get Vim to submit to adjusting my gap or any other thoughts on the best way to deal with this behavior?

+7
source share
1 answer

You do not set the string. Without it, vim ignores the break variable. For more details see here and a lot of good vim. Also note that you need to install nolist when it breaks a string.

Another word wrap blog post here . This notes that some file types do not include the "t" option flag. Without it, automatic packaging does not occur.

I think the easiest way would be to add to the display for launch :%! pythonTidy :%! pythonTidy . pythonTidy is a script in python to take the python code on stdIn and print a nice version in stdOut. With this mapping, it will allow you to run it in your current file and replace all the contents with a reformatted version. Add an autocommand to run it when you exit insert mode in python files and you need to be installed.

+3
source

All Articles