Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap long strings sublimetext python

Is there some magical way to have SublimeText handle a line like this automatically for me?

print('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent dapibus mauris urna, in semper dolor semper eget. Proin condimentum est sed est elementum, eu pulvinar eros malesuada. Quisque malesuada sapien et quam convallis, et sodales risus blandit. Vestibulum auctor justo eu libero pellentesque tempor. Quisque faucibus augue eu fermentum auctor.')

I'm honestly indifferent of it coming out like this:

print('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent dapibus'
      'mauris urna, in semper dolor semper eget. Proin condimentum est sed est'
      'elementum, eu pulvinar eros malesuada. Quisque malesuada sapien et quam'
      'convallis, et sodales risus blandit. Vestibulum auctor justo eu libero'
      'pellentesque tempor. Quisque faucibus augue eu fermentum auctor.')

or like this:

print(
    'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent dapibus'
    'mauris urna, in semper dolor semper eget. Proin condimentum est sed est'
    'elementum, eu pulvinar eros malesuada. Quisque malesuada sapien et quam'
    'convallis, et sodales risus blandit. Vestibulum auctor justo eu libero'
    'pellentesque tempor. Quisque faucibus augue eu fermentum auctor.'
)

or any other 'pythonic' way to handle a long string. Basically I want to write a long string, highlight it, and hit a button and find it is magically formatted some acceptable way. I would also really like to have some way to edit this string once it's already made without doing the work all over again.

Currently what I do is write this string, use Wrap->Wrap at X characters, then go through manually and add quotes to the start and end of each line and also adjust the indenting if necessary. If I want to edit, I have to then go and remove the starting and ending quotes in each line, concatenate it to one line, fix it, then do it all over again. It's awful.

like image 820
profesor_tortuga Avatar asked Dec 24 '13 12:12

profesor_tortuga


People also ask

How do I wrap in Sublime Text?

To wrap your selected content with a html tag, just selected your text, then press Alt-Shift-W.

How do you break a line in Sublime Text?

Select the paragraph and Ctrl+J.


1 Answers

It is possible to make multiple single-quoted lines like in your question, but you need to join them in the print statement:

print('long line 1' + \
      'long line 2' + \
      ...
      )

and it'd be a huge amount of work to undo that every time you wanted to edit the strings. So, why not just use a triple-quoted string?

print("""Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent
dapibus mauris urna, in semper dolor semper eget. Proin condimentum est sed
est elementum, eu pulvinar eros malesuada. Quisque malesuada sapien et quam
convallis, et sodales risus blandit. Vestibulum auctor justo eu libero
pellentesque tempor. Quisque faucibus augue eu fermentum auctor.""")

First, enter your mega-text on one line. Then, select it (I use the Expand Selection to Quotes plugin and just hit Ctrl') and hit AltQ to wrap. If you need to edit the string, select all the text and hit CtrlJ to join the lines back into one. When you're done editing, you can rewrap.

In my preferences I have

"word_wrap": "true",
"wrap_width": 0

so that even a single long string will automatically wrap like so:

wordwrap

Good luck!

like image 71
MattDMo Avatar answered Oct 04 '22 23:10

MattDMo