Remove/Add Line Breaks after Specific String using Sublime Text

StringText EditorSublimetext2

String Problem Overview


Using Sublime Text 2 - Is it possible to insert a line break/text return after a specific String in a text file e.g. by using the FindReplace tool?

(Bonus question: Is it possible to remove all line breaks after a specific String)

String Solutions


Solution 1 - String

Here's how you'd do it on a Mac:

Command+F > type string > Control+Command+G > ESC > Right Arrow > line break


and Windows/Linux (untested):

Control+F > type string > Alt+F3 > ESC > Right Arrow > line break


The important part being Control+Command+G to select all matches.
Once you've selected the text you're looking for, you can use the provided multiple cursors to do whatever text manipulation you want.

Protip: you can manually instantiate multiple cursors by using Command+click (or Control+click) to achieve similar results.

Solution 2 - String

Using the Find - Replace tool, this can be accomplished in two different ways:

  1. Click in the Replace field and press Ctrl + Enter to insert a newline (the field should resize but it doesn't, so it is hard to see the newline inserted).

  2. Inside the Find - Replace tool, activate the S&R regex mode (first icon on the left .*, keyboard shortcut is Alt + Ctrl/Cmd + R to activate/deactivate it).

  3. Type \n in the Replace field wherever you want to insert a newline.

Both solutions also work if you want to find newlines, just do it in the Find field.

Solution 3 - String

Edit->Lines->Join Line (Ctrl+J)

Solution 4 - String

You should probably use multiple cursors. See the unofficial documentation, or this nice tutorial. Here's some brief instructions to set you on your way:

  1. Put the cursor on the string of interest.
  2. Type Command+D (Mac) or Control+D (Windows/Linux) to select the current instance of the string.
  3. Type Command+D (Mac) or Control+D (Windows/Linux) to select successive instances of the string.
  4. Alternately, type Control+Command+G (Mac) or Control+Command+G to select all instances of your string.
  5. Now you have multiple cursors, so insert or remove your newline as you please.
  6. (type esc to exit multiple cursor mode.)

Have fun!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBerndView Question on Stackoverflow
Solution 1 - StringSaraView Answer on Stackoverflow
Solution 2 - StringveducmView Answer on Stackoverflow
Solution 3 - StringOtpidusView Answer on Stackoverflow
Solution 4 - StringdbnView Answer on Stackoverflow