Merge multiple lines into one using Sublime Text

Sublimetext2Sublimetext3Sublimetext

Sublimetext2 Problem Overview


I am very new to Sublime Text and I am sure this is a naive question. Watching the Multiple line selection at http://www.sublimetext.com/ (2/6 slide). Absolutely love it.

I understand that Ctrl (Cmd)+Shift+L "multiple-selects" so that we could do the edit simultaneously. However, in the demo, they also merge all the lines into 1 single line. What is the shortcut for that?
I used Ctrl+J but it just deletes once and not all the occurrences of new line.

I use TextPad and use Find/Replace \n with an empty space. But it seems that the person giving the demo uses some kind of shortcut.

Sublimetext2 Solutions


Solution 1 - Sublimetext2

A single command shortcut for merging multiple lines into 1 is "join lines".

  • Command + Shift + J on the Mac to join lines.
  • CTRL + Shift + J on Windows
  • Edit > Lines > Join Lines

Important note: This keyboard shortcut changed in versions of sublime text released after around mid-2021. For older versions, use Command + J, or CTRL + J.

Another approach is seen in the demo animations on sublimetext.com. Using multiple selections, Ctrl+Shift+L is used to split a selection into lines, and each line is then edited simultaneously. end + del will then remove all line breaks. This can be seen in slide 2/6 at http://sublimetexttips.com/7-handy-text-manipulation-tricks-sublime-text-2/

Solution 2 - Sublimetext2

I think that, in the demo, he presses Del, with the cursor at the end of the line, in multiple selection. This way the \n is removed in every selected line.

Solution 3 - Sublimetext2

ctrl+a and ctrl+j seems working on sublime text 3.

Solution 4 - Sublimetext2

Editing

join Joins the following line to the current line, replacing all in between whitespace with a single space

http://www.sublimetext.com/docs/commands

Go to edit option in menu, Edit -> line -> join lines

Solution 5 - Sublimetext2

Select the lines you want to merge and press Ctrl + j and if you want to make all file in 1 line then do Ctrl + a and Ctrl + j.

Solution 6 - Sublimetext2

If you want to merge lines into one line that will also remove the starting, and ending space from the line, the following regex should work:

Find What: ^\s*(.+)\s*\n
Replace With: \1

Solution 7 - Sublimetext2

Join lines is a good command, but it adds spaces between the merged lines. To merge lines without spacing, the easiest way appears to be the following:

  1. Find -> Replace (Command+Option+F on Mac)
  2. Ctrl+Enter to enter newline to the Find What field.
  3. Don't enter anything into the Replace With field.
  4. Press Replace All.

Solution 8 - Sublimetext2

I have been using a regex approach in Sublime Text 3, as follows:

  • Press Ctrl+H (in Windows) to show the "Find and Replace" dialog at the bottom.
  • In the "Find" field, use $\n\s* (end of line, carriage return and any arbitrary number of spaces following, including zero, which should be at the beginning of the next line).
  • The "Replace" field should be empty.
  • Hit "Replace All" or Alt+Ctrl+Enter.

This should do the trick!

Solution 9 - Sublimetext2

Sublime Text 3 for Mac:

cmd + j no longer works and is now CMD + SHIFT + J

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
QuestionArun ManivannanView Question on Stackoverflow
Solution 1 - Sublimetext2urocView Answer on Stackoverflow
Solution 2 - Sublimetext2Riccardo MarottiView Answer on Stackoverflow
Solution 3 - Sublimetext2yılmazView Answer on Stackoverflow
Solution 4 - Sublimetext2user3163041View Answer on Stackoverflow
Solution 5 - Sublimetext2Donald ShahiniView Answer on Stackoverflow
Solution 6 - Sublimetext2dardoView Answer on Stackoverflow
Solution 7 - Sublimetext2Dennis GolomazovView Answer on Stackoverflow
Solution 8 - Sublimetext2pcesarperezView Answer on Stackoverflow
Solution 9 - Sublimetext2DapView Answer on Stackoverflow