97

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)

Bernd
  • 11,133
  • 11
  • 65
  • 98

4 Answers4

225

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.

Sara
  • 8,222
  • 1
  • 37
  • 52
  • 1
    THis seems to break down when I try it with regular expression searches like `(this|that|the other)` – Amanda Jun 06 '13 at 21:16
  • 1
    @Amanda try Option+Enter instead of ESC (shortcut for the "Find All" button). – Sara Jun 07 '13 at 02:00
  • 1
    It's worth mentioning this same tactic works great for removing line breaks that end with a particular pattern, such as binary blobs in Windows .reg files: instead of right arrow + line break, try left arrow + delete until you get the union you want. Also, your Windows hotkeys are correct. I don't have a Linux instance handy to test with, but it's probably fine. – Tohuw Jul 25 '14 at 13:08
  • Any clue on how to do this on a selection instead of the whole file? Control+Command+G clears the selection and finds all in the whole file. – Mauro Apr 27 '15 at 14:59
  • 2
    Seems so obvious now... thanks a million! Also, @Mauro - I'm on Windows, and I can used ctrl + d to select the next iteration of the selection. Perhaps the same functionality exists on MAC with Command + d ? You may have to press it numerous times to go through the whole file – shanehoban Apr 30 '15 at 14:51
  • @shanehoban Ohh! That was it. I changed the Cmd+D shortcut a long ago to delete a complete line like I do on Eclipse. – Mauro Apr 30 '15 at 18:55
  • The Ctrl-CMD-G plus left/right arrow is a magic! :) I would never image it could be used this way. Thanks for sharing! – LeOn - Han Li Mar 24 '16 at 15:21
69

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.

veducm
  • 5,933
  • 2
  • 34
  • 40
  • Can't find the S&R regex mode but the first solution works well! – hhh Nov 19 '14 at 09:46
  • @hhh: If you open the ***Find-Replace tool***, ***S&R regex mode*** is the first icon on the left of the tool menu (looks like .*) – veducm Nov 19 '14 at 20:23
  • @veducm Yes I can see that icon corresponding to the search field but not for the replace field. Maybe it is because I have an unregistered version? – hhh Nov 19 '14 at 21:19
  • 1
    @hhh there is only one icon and when enabled it applies to both fields. – veducm Nov 19 '14 at 21:49
  • @veducm thanks. finally an easy solution to an easy problem. You know why we need the regular expression enabled though? for the replacing string – sethi Sep 17 '20 at 01:38
8

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

Otpidus
  • 489
  • 5
  • 5
4

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!

dbn
  • 13,144
  • 3
  • 60
  • 86