557

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.

leemour
  • 11,414
  • 7
  • 36
  • 43

13 Answers13

967

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

Nimantha
  • 6,405
  • 6
  • 28
  • 69
chrisg86
  • 11,548
  • 2
  • 16
  • 30
  • 2
    +1 to @ShahzadTariq as this doesn't work on Ubuntu Sublime, however the answer below from Sash does the trick just fine – Paul Carroll Dec 08 '17 at 00:15
  • 12
    To turn on Regex Search and Replace on Mac: `⌘` + `⌥` + `R` :) – pmalbu Oct 25 '18 at 02:31
  • If this doesn't appear to be working, you might want to try replacing \n with your target string -- \\n replaces the actual sequence "\n" in the text content within your document, whereas \n replaces actual line breaks in the document. – duhaime Feb 18 '19 at 14:52
  • Thanks, my Sublime died when I clicked Replace after almost 2'000'000 matches found – SeniorJD Jun 09 '20 at 12:37
  • 3
    @SeniorJD sublime is pretty lightweight when it comes to handling large files. Maybe you could try something like `sed -i 's/\\n/\n/g' your_file.txt` which I guess could be more resource friendly. – barell Sep 01 '20 at 15:34
  • Doesn't work for me. This finds "\\n" text instead and replaces it with text "\n" Sash's answer works, however! – Erika Electra Jul 21 '22 at 06:31
348

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.

garyh
  • 2,782
  • 1
  • 26
  • 28
Sash
  • 4,448
  • 1
  • 17
  • 31
64

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)

enter image description here

Heinrich Cloete
  • 702
  • 5
  • 12
Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65
25

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)

Nimantha
  • 6,405
  • 6
  • 28
  • 69
nhnminh
  • 371
  • 3
  • 3
21

In Sublime Text (with shortcuts on Mac):

  1. Highlight the text that you want to search to apply Find & Replace

  2. Go to Menu > Find > Replace... (Keyboard Shortcut: Alt + Command + F)

  3. In the Find & Replace tool, enable Regular Expression by clicking on the button which looks like [.*] (Keyboard Shortcut: Alt + Command + R)

  4. In Find What, type: \\n

    Note: The additional \ escapes the Regular Expression syntax when searched.

  5. In Replace With, type: \n

  6. Click on the 'Replace All' button (Keyboard Shortcut: Ctrl + Alt + Enter)

Your literal text \n will then turn into an actual line break.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
allisondev
  • 211
  • 2
  • 3
12

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.

Feng Liu
  • 954
  • 13
  • 22
10
  1. Open Find and Replace Option ( Ctrl + Alt + F in Mac )
  2. Type \n in find input box
  3. Click on Find All button, This will select all the \n in the text with the cursor
  4. Now press Enter, this will replace \n with the New Line
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Hassan Mudassir
  • 191
  • 2
  • 10
  • Taking significant amount of time for large files but works for me while other answers didn't. Thank you – Volod Jun 16 '22 at 17:30
7

For Windows line endings:

(Turn on regex - Alt+R)

Find: \\r\\n

Replace: \r\n

illustration

Nimantha
  • 6,405
  • 6
  • 28
  • 69
jnt
  • 1,133
  • 14
  • 10
7

On Mac, Shift+CMD+F for search and replace. Search for \n and replace with Shift+Enter.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Joris Weimar
  • 4,783
  • 4
  • 33
  • 53
6

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.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
pid
  • 11,472
  • 6
  • 34
  • 63
  • drag to the following empty line -> you can do the same doing 3 clicks over the line you want to copy (over text, over tabs works also, but no with spaces ) – m3nda Apr 26 '17 at 21:24
4

The easiest way, you can copy the newline (copy empty 2 line in text editor) then paste on replace with.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
3

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.

replace field

Nimantha
  • 6,405
  • 6
  • 28
  • 69
1

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

HIZIR
  • 17
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 23 '22 at 07:01