Merge multiple lines into one using Sublime Text - sublimetext2

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.

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/

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.

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

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

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.

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

Sublime Text 3 for Mac:
cmd + j no longer works and is now CMD + SHIFT + J

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.

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!

Related

notepad++ remove new line in " " at my csv-file

My problem is that I have a csv-file with alot of new line. How can I remove the newlines with select and replace? My other stuff should stay how it is.
Here the example:
101080;101080;101080;104;
101098;101098;101098;105;
101099;101099;101099;106;"RING 750GG 1BRI TW VS 0,0300ct 1 RUBIN WEITE58.0
BREITE 4.5mm
72-91872-0-0 045-71-0-58-3
704.- VP1000.-
";
1011;1011;1011;106;
101093;101093;101093;123;
I have all over the csv file these new lines. I need to make that in 1 line.
thank you for the help
If I understand the question correctly, you only wish to remove the newlines for any lines that do not end in a semicolon character. You can do that below with this Find and Replace in Notepad ++ using the Regular Expression Search Mode.
Find: ([^;]$)(\r\n)
Replace: \1
Explanation of the find is that it is looking for the end of line and newline character that isn't preceded by a semicolon character. The parenthesis tell the find to group the results, which allows us in the replace to just keep the first grouping of find results and to discard the newline characters for these lines that don't end in a semicolon, which will then end up making it all line up on the previous line.
Here is a simple solution:
Activate the ShowAll Icon (¶) in the toolbar of Notepad++
Mark a ; and NewLine-Sequence (CR LF) in your file
Use the shortcut Ctrl + F
Select the Replace tab in the appeared search window
Insert ; in the Replace with text box
Press Replace All
Now all items ending with a ; are in single rows.
To remove all new lines: Just replace "\r\n" (windows) or "\n" (unix) with nothing! Using Extended or Regex options.
To just remove some:
First add unique data where you want to keep the new line - I use "QAZ".
Then remove all new lines as above.
Then replace "QAZ" with "\n".

Notepad++ tabs to spaces everywhere other than beginning of line

Due to a new coding style that I've been having to use I'm required to use tabs in the beginning of lines but spaces everywhere else to align things.
Is there a way to customize notepad++ to only replace tabs with spaces if it's not at the beginning of a new line?
Just as an example of what I mean I'll use this bit of 'code':
function someFunction():
while(true):
veryLongCodeStuff() // Some comment
shortCode() // Aligned comment
Which I would have to write like this (where \t = tab and a "." represents a space):
function someFunction():
\twhile(true):
\t\tveryLongCodeStuff()..// Some comment
\t\tshortCode()..........// Aligned comment
To convert existing files, I would suggest a two step approach:
replace all tabs with spaces (this can be done with Edit -> Blank operations -> TAB to Space )
replace spaces at the beginning of lines: do a regular expression find/replace like this:
Open Replace Dialog
Find What: ^([\t]?)( ){4} instead of 4 use the number of spaces you have configured as tab width
Replace With: \1\t
check regular expression
click Replace All: as each Replace All replaces only one indent level for all lines: repeat until the status bar of the find dialog tells you, that no more replacements were done (just keep Alt-A pressed for a second or two)

How to remove line break and all spaces on the next line in sublime text editor?

It happens many times when we need to remove line end and unnecessary space between the HTML element. So this could be proven as a time-saving technique.
open Search and replace (CTRL+H)
enable "Regular Expression" (circled in red)
enter \n\s+ in the search field, clear the replace field
press "Replace all"
This will remove all newlines (\n+) followed by all whitespace (\s+) in the next line.
In your Sublime Text user settings, you can specify the following to trim trailing white space.
"trim_trailing_white_space_on_save": true
You might want to have a look at the Trimmer that has a couple of options to trim whitespace.
you can also use \n+ to remove all the unwanted space , if you want to use a single line then replace with \n online

Replace \n with actual new line in Sublime Text

How can I replace \n in Sublime Text with real in-editor displayed new line so:
foo\nbar
becomes:
foo
bar
in the editor when I view the file in it.
Turn on Regex Search and Replace (icon most to the left in search and replace bar or shortcut Alt + R)
Find What: \\n
Replace with: \n
Use Find > Replace, or (Ctrl+H), to open the Find What/Replace With Window, and use Ctrl+Enter to indicate a new line in the Replace With inputbox.
Fool proof method (no RegEx and Ctrl+Enter didn't work for me as it was just jumping to next Find):
First, select an occurrence of \n and hit Ctrl+H (brings up the Replace... dialogue, also accessible through Find -> Replace... menu). This populates the Find what field.
Go to the end of any line of your file (press End if your keyboard has it) and select the end of line by holding down Shift and pressing → (right arrow) EXACTLY once. Then copy-paste this into the Replace with field.
(the animation is for finding true new lines; works the same for replacing them)
On Windows, Sublime text,
You press Ctrl + H to replace \n by a new line created by Ctrl + Enter.
Replace : \n
By : (press Ctrl + Enter)
In Sublime Text (with shortcuts on Mac):
Highlight the text that you want to search to apply Find & Replace
Go to Menu > Find > Replace... (Keyboard Shortcut: Alt + Command + F)
In the Find & Replace tool, enable Regular Expression by clicking on the button which looks like [.*] (Keyboard Shortcut: Alt + Command + R)
In Find What, type: \\n
Note: The additional \ escapes the Regular Expression syntax when searched.
In Replace With, type: \n
Click on the 'Replace All' button (Keyboard Shortcut: Ctrl + Alt + Enter)
Your literal text \n will then turn into an actual line break.
What I did is simple and straightforward.
Enter Space or \n or whatever you want to find to Find.
Then hit Find All at right bottom corner, this will select all results.
Then hit enter on your keyboard and it will break all selected into new lines.
Open Find and Replace Option ( Ctrl + Alt + F in Mac )
Type \n in find input box
Click on Find All button, This will select all the \n in the text with the cursor
Now press Enter, this will replace \n with the New Line
On Mac, Shift+CMD+F for search and replace. Search for \n and replace with Shift+Enter.
None of the above worked for me in Sublime Text 2 on Windows.
I did this:
click on an empty line;
drag to the following empty line (selecting the invisible character after the line);
press Ctrl+H for replace;
input desired replacement character/string or leave it empty;
click "Replace all";
By selecting before hitting Ctrl+H it uses that as the character to be replaced.
For Windows line endings:
(Turn on regex - Alt+R)
Find: \\r\\n
Replace: \r\n
The easiest way, you can copy the newline (copy empty 2 line in text editor) then paste on replace with.
On MAC:
Step 1: Alt + Cmd + F . At the bottom, a window appears
Step 2: Enable Regular Expression. Left side on the window, looks like .*
Step 3: Enter text to you want to find in the Find input field
Step 4: Enter replace text in the Replace input field
Step 5: Click on Replace All - Right bottom.
ctrl+h
sample :
type: </>
replace: \n
down below , find button - change to ALT+R
you see changed eFFect in color , better to use sample
nice feature

Insert same code around different filenames

I am making a small online database that is accessible through the form of checkboxes for download. I was wondering if there was some way to list all of the filenames available for download in Sublime Text 2 and insert the same code around each filename?
Everything is functional, it would just save me a lot of repetitive copy and pasting if there is a faster way to do this.
Use SublimeText Find & Replace. Click the Regex button (it looks like a * to the left of the search box)
In the Find box, insert: (^.*$)
In the replace box: [yourcode]$1[yourcode]
Where [yourcode] is what you want to insert into the box.
So, if you want to make them all <li> then your replace box would be:
<li>$1</li>
Remember to use escape \ characters where they are needed, in case you need to insert restricted characters.
^ - beginning of a new line.
. - wildcard
* - any number of the previous character in the sequence (in this case a wildcard, so any text)
$ - the end of a line
() - denotes a block, it's how the $1 knows what text to put in it's place.
Sublime Text Search and Replace
Use search/replace on a text editor with regular expressions.
^ and $ represent the beginning and end of a line - thus allowing you to easily surround each line with the appropriate text.
Sometimes you can copy the newline character (as in copy the end of one line to the beginning of the next line), and replace that with whatever text you need.
You could always use the regular expression search / replace feature in Notepad++.