Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: gq ignores open quotes when wrapping text

Tags:

python

vim

Suppose I have a list of strings in a file (a Python script, for example) like this:

my_list_of_numbers = ["one two",
    "three four",
    "five six",
    "seven eight",
    "nine ten",
    "eleven twelve"]

If I make a visual selection of this text, I can use gq to wrap the text to get:

my_list_of_numbers = ["one two", "three four", "five six", "seven eight", "nine
    ten", "eleven twelve"]

However, the element "nine ten" is now split in half over two lines.

How do I wrap the text while avoiding entering a newline character inside an open quote? My desired result would look like this:

my_list_of_numbers = ["one two", "three four", "five six", "seven eight",
    "nine ten", "eleven twelve"]
like image 776
maccruiskeen Avatar asked Dec 03 '15 22:12

maccruiskeen


1 Answers

I didn't think that there is a reserved command to do this, but we can do it by make it to one line and then by regex, break it to your desired wish.

%s/\(.\{-}".\{-}".\{-}".\{-}".\{-}".\{-}".\{-}\)"/\1\r"/gec

just map the whole thing into a key. so as to make you easy.

like image 141
Zam Avatar answered Nov 07 '22 12:11

Zam