ChatGPT解决这个技术问题 Extra ChatGPT

Merge multiple lines into one using Sublime Text

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.


C
Cheekysoft

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/


And if you're looking for it in the the menu's, its under Edit > Lines > Join Lines
Thanks. cmd+J is working properly in Mac Sublime version. It join the lines with a space. Like: 1. Hello 2. world after cmd + J it becomes: 1. Hello world
Probably it is obvious, but still - you can select any part of the text and use this combination applying to the selected text.
Cool - reminding "J" vi command. Sublime authors are really smart!
Recent sublime version on mac has shortcut command+shift+j.
R
Riccardo Marotti

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.


LOL. You are right. I just had my wordwrap "on" and was bothered.
This is clever, but adds whitespace when lines are uneven in length. The Command/Ctrl + J solution is better.
-1 Doing this with thousands of lines selected will kill your CPU and memory
y
yılmaz

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


u
user3163041

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


D
Donald Shahini

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.


d
dardo

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

D
Dennis Golomazov

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:

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


p
pcesarperez

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!


D
Dap

Sublime Text 3 for Mac:

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