Question

I often get text from clients and want to quickly format the text with html tags and wrap each text line at a specified number of characters. I've just installed Sublime Text 2 and it's pretty nice, but one of the things I really want to do I can't quite figure out.

I want to take long paragraphs, wrap each paragraph in a paragraph p tag, and then wrap the lines so they don't run off the screen. So here's what I'm doing:

  1. Copy and paste text from my client into editor (2 paragraphs for this example).

  2. Select text.

  3. Using Emmet, enter "p*" which puts p tags at the beginning of each paragraph and /p at the end of each paragraph.

  4. Select text.

  5. Click Alt Q to wrap text.

The text wraps but it's corrupted because the opening angle bracket "<" from the /p tag is appended to the beginning of each line and the opening angle bracket is missing from the /p tag.

<p>Our swimming lessons run on a perpetual monthly enrollment system,
<making year-round lessons affordable and convenient. Our online
<registration system allows you to sign up at your convenience and
<monitor your account details easily./p>

<p>Our highly trained swim instructors teach our unique, proven
<curriculum in stages, encouraging swimmers to master the fundamentals
<of every important swimming skill. We continuously encourage
<progression and advancement as each swimmer becomes more confident in
<the water. Our program blends important water safety skills, buoyancy
<principles and correct stroke technique./p>

Help! What am I doing wrong?

Was it helpful?

Solution

Here's what you can do:

  1. Paste content from client.
  2. Select, hit AltQ to wrap. You'll now have two cursors, one at the end of each paragraph.
  3. Select Selection -> Expand Selection to Paragraph (I'll show you how to make a shortcut later). Both paragraphs are now selected, each as a selection region.
  4. Bring up Emmet with CtrlShiftG and enter p (not p*)
  5. Hit Enter and you should have two wrapped paragraphs surrounded by <p></p> tags:
<p>Our swimming lessons run on a perpetual monthly enrollment system, making
    year-round lessons affordable and convenient. Our online registration system
    allows you to sign up at your convenience and monitor your account details
    easily.</p>
<p>Our highly trained swim instructors teach our unique, proven curriculum in
    stages, encouraging swimmers to master the fundamentals of every important
    swimming skill. We continuously encourage progression and advancement as each
    swimmer becomes more confident in the water. Our program blends important
    water safety skills, buoyancy principles and correct stroke technique.</p>

To create a keyboard shortcut for Expand Selection to Paragraph, go to Preferences -> Key Bindings - User and add the following:

{ "keys": ["ctrl+alt+shift+p"], "command": "expand_selection_to_paragraph" }

If the file is empty, wrap the line above in square brackets []:

[
    { "keys": ["ctrl+alt+shift+p"], "command": "expand_selection_to_paragraph" }
]

Save the file, and you should now be able to use CtrlAltShiftP for step 3 above. Feel free to change the key combination if you wish, but be aware that it may conflict with other built-in or plugin combos.

Note: I tested all this on Sublime Text 3, but it should work the same in ST2.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top