Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep vim from breaking string literals?

This sounds like a very basic question that probably has an obvious solution, I haven't found it, unfortunately.

How do I keep vim from breaking (entering a newline) in the middle of a string? I type in a list of strings, and it looks like this:

a=['a string', 'b string', 'c
string']

I like the line breaks, I don't want vim just to wrap the line and continue on the next editor line. I want my lines to be roughly 80 characters long without manually entering newlines.

What I want is either

  1. Don't break any string, even if it has a whitespace

  2. Break the string using implicit string concatenation, i.e. close the string and start a new string on the new line

  3. Break just before 'c string'

The closest solutions I have found are not very useful:

Is there any way to get vim to auto wrap python strings at 79 chars?

how to configure vim to smartly wrap python strings?

Vim: Inter-String Line Breaking

Currently 'J' is one of the most heavily used keys to fix broken strings.

like image 890
Dan Avatar asked Sep 21 '13 07:09

Dan


1 Answers

Here is what I do to wrap all lines at 80 characters without breaking any words, and preserve shorter lines:

:set formatoptions+=w
:set tw=80
gggqG

You can also use the following mapping to format current paragraph:

:nnoremap Q gqip

hope it helps.

like image 122
Stryker Avatar answered Oct 20 '22 10:10

Stryker