326

How can I find/replace all CR/LF characters in Notepad++?

I am looking for something equivalent to the ^p special character in Microsoft Word.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
polara
  • 4,722
  • 5
  • 21
  • 19
  • 1
    Though not related, but helpful. I visited this page after searching a method to convert UNIX text file to Windows format in Windows. This is what I did and worked, `CTRL+H`, select "Search Mode: Regular Expression". "Find What: $" and "Replace with : \r". Copy paste the content into Notepad or save it to disc. – Guru Jan 21 '12 at 01:47
  • 2
    Yay! Notepad++6.0 does bring the support of PCRE: regex like `[\r\n]+` now work. I have updated my answer, and will celebrate! – VonC Mar 26 '12 at 20:30
  • @VonC: I guess I need to update [all my \[notepad++\] answers](http://stackoverflow.com/search?q=user:106224+%5bnotepad%2b%2b%5d]) as well :) – BoltClock Mar 26 '12 at 20:44
  • Just to build a little on what 'thenonhacker' was mentioning. We use ULtraEdit-32 on the job, and working with EDI files (x.12), we often have to wrap or unwrap data. The 2 most common Search & Replace strings are "^p" & "~". going one way will replace the "~" with a CR/LF for easier reading of each segment. Going the other way puts the file back into the format the EDI translator needs. For lazy people like myself, I just have those map'd to keys for easy of use. – DavidGrove Jun 11 '10 at 13:05
  • Seems nobody has mentioned *escape sequence* [`\R`](https://www.regex101.com/r/4tYCK9/1) yet which [matches a generic newline; that is, anything considered a linebreak sequence...](http://perldoc.perl.org/perlrebackslash.html#Misc) – bobble bubble Dec 31 '16 at 02:12
  • Just FYI, no need to search and replace. At least as of Notepad ++ 7.3.3, you can Right Click on 'Windows (CR LF)' or 'Unix (LF)' in the bottom bar of the open Notepad ++ window and switch between the two. Success can be confirmed using the 'Show All Characters' button in the menu bar. – Caseyy Mar 30 '17 at 15:36

18 Answers18

421

[\r\n]+ should work too

Update March, 26th 2012, release date of Notepad++ 6.0:

OMG, it actually does work now!!!

PCRE regexp in Notepad++


Original answer 2008 (Notepad++ 4.x) - 2009-2010-2011 (Notepad++ 5.x)

Actually no, it does not seem to work with regexp...

But if you have Notepad++ 5.x, you can use the 'extended' search mode and look for \r\n. That does find all your CRLF.

(I realize this is the same answer than the others, but again, 'extended mode' is only available with Notepad++ 4.9, 5.x and more)


Since April 2009, you have a wiki article on the Notepad++ site on this topic:
"How To Replace Line Ends, thus changing the line layout".
(mentioned by georgiecasey in his/her answer below)

Some relevant extracts includes the following search processes:

Simple search (Ctrl+F), Search Mode = Normal

You can select an EOL in the editing window.

  • Just move the cursor to the end of the line, and type Shift+Right Arrow.
  • or, to select EOL with the mouse, start just at the line end and drag to the start of the next line; dragging to the right of the EOL won't work. You can manually copy the EOL and paste it into the field for Unix files (LF-only).

Simple search (Ctrl+F), Search Mode = Extended

The "Extended" option shows \n and \r as characters that could be matched.
As with the Normal search mode, Notepad++ is looking for the exact character.
Searching for \r in a UNIX-format file will not find anything, but searching for \n will. Similarly, a Macintosh-format file will contain \r but not \n.

Simple search (Ctrl+F), Search Mode = Regular expression

Regular expressions use the characters ^ and $ to anchor the match string to the beginning or end of the line. For instance, searching for return;$ will find occurrences of "return;" that occur with no subsequent text on that same line. The anchor characters work identically in all file formats.
The '.' dot metacharacter does not match line endings.

[Tested in Notepad++ 5.8.5]: a regular expression search with an explicit \r or \n does not work (contrary to the Scintilla documentation).
Neither does a search on an explicit (pasted) LF, or on the (invisible) EOL characters placed in the field when an EOL is selected. Advanced search (Ctrl+R) without regexp

Ctrl+M will insert something that matches newlines. They will be replaced by the replace string.
I recommend this method as the most reliable, unless you really need to use regex.
As an example, to remove every second newline in a double spaced file, enter Ctrl+M twice in the search string box, and once in the replace string box.

Advanced search (Ctrl+R) with Regexp.

Neither Ctrl+M, $ nor \r\n are matched.


The same wiki also mentions the Hex editor alternative:

  • Type the new string at the beginning of the document.
  • Then select to view the document in Hex mode.
  • Select one of the new lines and hit Ctrl+H.
  • While you have the Replace dialog box up, select on the background the new replacement string and Ctrl+C copy it to paste it in the Replace with text input.
  • Then Replace or Replace All as you wish.

Note: the character selected for new line usually appears as 0a.
It may have a different value if the file is in Windows Format. In that case you can always go to Edit -> EOL Conversion -> Convert to Unix Format, and after the replacement switch it back and Edit -> EOL Conversion -> Convert to Windows Format.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • 1
    I was running v 4.7. Upgraded to the newest version and extended mode works as advertised. Thanks to all who responded. – polara Sep 25 '08 at 18:10
  • +1: I was running 4.6, and had a similar problem. And thanks to your answer, I upgraded to 5+. Thanks! – paercebal Mar 15 '10 at 13:22
  • It works! But this makes Notepad++ RegExp suck. UltraEdit-32 is smarter, you just need \n and you've finished your job... – thenonhacker Mar 29 '10 at 14:35
  • \r\n worked for me, but not \n, which is kind of counter-intuitive if you're a developer who is used to \n being shorthand for \r\n. – Warren P May 26 '10 at 20:19
  • For me, Ctrl M didn't seem to do anything. Searched for (\d+)$ with Ctrl M between ) and $. Replaced with '\1, ' and the newline character was not replaced but everything else was. v 5.9.2 – jcollum Oct 24 '11 at 17:13
  • FWIW, the windows newline format is CR LF, 0D 0A in hex (the hex one listed is 0A, just LF, compatible with unix) – Matt Lyons Dec 20 '11 at 02:37
  • So how can I search for LF that isn't preceded by a CR? I tried `[^\r]\n` but that didn't seem to work. – Garret Wilson Dec 01 '15 at 02:25
  • 2
    @GarretWilson I confirm `[^\r]\n` works just fine (regex search): http://i.stack.imgur.com/PxZWD.png – VonC Dec 01 '15 at 05:08
  • Thanks, @VonC. I too have confirmed it works. I think I was accidentally using "Extended" instead of "Regular Expression" search mode earlier. – Garret Wilson Dec 01 '15 at 17:15
23

It appears that this is a FAQ, and the resolution offered is:

Simple search (Ctrl+H) without regexp

You can turn on View/Show End of Line or view/Show All, and select the now visible newline characters. Then when you start the command some characters matching the newline character will be pasted into the search field. Matches will be replaced by the replace string, unlike in regex mode.

Note 1: If you select them with the mouse, start just before them and drag to the start of the next line. Dragging to the end of the line won't work.

Note 2: You can't copy and paste them into the field yourself.

Advanced search (Ctrl+R) without regexp

Ctrl+M will insert something that matches newlines. They will be replaced by the replace string.

Community
  • 1
  • 1
sundar - Remember Monica
  • 6,647
  • 5
  • 40
  • 66
11

On the Replace dialog, you want to set the search mode to "Extended". Normal or Regular Expression modes wont work.

Then just find "\r\n" (or just \n for unix files or just \r for mac format files), and set the replace to whatever you want.

Nathen Silver
  • 429
  • 4
  • 7
  • This answer was outdated when written - Mac hasn't been using `\r` since 2001... (it's unix now) – Jasper Aug 12 '14 at 14:26
9

I've not had much luck with \r\n regular expressions from the find/replace window.

However, this works in Notepad++ v4.1.2:

  1. Use the "View | Show end of line" menu to enable display of end of line characters. (Carriage return line feeds should show up as a single shaded CRLF 'character'.)

  2. Select one of the CRLF 'characters' (put the cursor just in front of one, hold down the SHIFT key, and then pressing the RIGHT CURSOR key once).

  3. Copy the CRLF character to the clipboard.

  4. Make sure that you don't have the find or find/replace dialog open.

  5. Open the find/replace dialog. The 'Find what' field shows the contents of the clipboard: in this case the CRLF character - which shows up as 2 'box characters' (presumably it's an unprintable character?)

  6. Ensure that the 'Regular expression' option is OFF.

Now you should be able to count, find, or replace as desired.

6

Image with CRLF

enter image description here


Image without CRLF

enter image description here

Bhargav Rao
  • 41,091
  • 27
  • 112
  • 129
4

In 2013, v6.13 or later, use:

Menu EditEOL ConversionWindows Format.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Aquarius Power
  • 3,212
  • 5
  • 26
  • 56
  • This does not cover the general case of replacing CRLF with one or more arbitrary characters. Or replacing one or more arbitrary characters with CRLF (say replace space characters with CRLF). – Peter Mortensen Jun 13 '18 at 16:24
4

The way I found it to work is by using the Replace function, and using "\n", with the "Extended" mode. I'm using version 5.8.5.

Chris
  • 41
  • 1
  • 1
    One of the very few items on this page that works. Using notepadd++ 5.8.6. Its really hard to believe something this simple should require the amount of overly complex and often non working answers that has been given in this thread. – Andrew S May 11 '13 at 21:17
2

Assuming it has a "regular expressions" search, look for \r\n. I prefer \r?\n, because some files don't use carriage returns.

EDIT: Thanks for the feedback, whoever voted this down. I have learned that... well, nothing, because you provided no feedback. Why is this wrong?

Chris Marasti-Georg
  • 32,594
  • 15
  • 88
  • 130
  • 1
    I'm not sure exactly why it doesn't work but it doesn't. I'm assuming that this regex implementation (in Notepad++) only checks a single line at a time and ignores any new line characters. That's probably why you were voted down. – frank hadder Apr 29 '10 at 00:33
  • 1
    yep. regex is line by line in notepad++, so you can match positionally at EOL with $ but can not search/replace expressions that cross line boundaries. – Warren P May 26 '10 at 20:21
2

Use the advanced search option (Ctrl + R) and use the keyboard shortcut for CRLF (Ctrl + M) to insert a carriage return.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • Ctrl + R didn't work for me when I tried it. It inserted a strange control character in the document, "`DC2`". Notepad++ version: 7.5.6 (2018-05-19). – Peter Mortensen Jun 13 '18 at 16:09
2

Go to View--> Show symbol-->Show all character // Its worked for me

2

If you need to do a complex regexp replacement including \r\n, you can workaround the limitation by a three-step approach:

  1. Replace all \r\n by a tag, let's say #GO# → Check 'Extended', replace \r\n by #GO#
  2. Perform your regexp, example removing multiline ICON="*" from an html bookmarks → Check regexp, replace ICON=.[^"]+.> by >
  3. Put back \r\n → Check 'Extended', replace #GO# by \r\n
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
alex
  • 21
  • 1
1

I opened the file in Notepad++ and did a replacement in a few steps:

  1. Replace all "\r\n" with " \r\n"
  2. Replace all "; \r\n" with "\r\n"
  3. Replace all " \r\n" with " "

This puts all the breaks where they should be and removes those that are breaking up the file.

It worked for me.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Cognis
  • 65
  • 1
  • 2
  • 9
1

Make this setting. Menu-> View-> Show Symbol-> uncheck Show End of the Line

  • 1
    This does not answer the original question. Please explain why you think this is a useful contribution to this Q&A. Please also read the [help] pages about how to make useful answers. – AdrianHHH Mar 21 '17 at 11:43
1

I was totally unable to do this in NP v6.9. I found it easy enough on Msoft Word (2K).

Open the doc, go to edit->replace.

Then in the bottom of the search box, click "more" then find the "Special" button and they have several things for you. For Dos style, I used the "paragraph" one. This is a cr lf pair in windows land.

Cool Javelin
  • 626
  • 2
  • 8
  • 18
0

Maybe you can use TextFX plugins

In TextFX, go to textfx edit → delete blank lines

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
0

Just do a \r with a find and replace with a blank in the replace field so everything goes up to one line. Then do a find and replace (in my case by semi colon) and replace with ;\n

:) -T&C

Trent
  • 11
0

To find any kind of a line break sequence use the following regex construct:

\R

To find and select consecutive line break sequences, add + after \R: \R+.

Make sure you turn on Regular expression mode:

enter image description here

It matches:

  • U+000DU+000A -CRLF` sequence
  • U+000A - LINE FEED, LF
  • U+000B - LINE TABULATION, VT
  • U+000C - FORM FEED, FF
  • U+000D - CARRIAGE RETURN, CR
  • U+0085 - NEXT LINE, NEL
  • U+2028 - LINE SEPARATOR
  • U+2029 - PARAGRAPH SEPARATOR
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
0

To change a document of separate lines into a single line, with each line forming one entry in a comma separated list:

  1. ctrl+f to open the search/replacer.
  2. Click the "Replace" tab.
  3. Fill the "Find what" entry with "\r\n".
  4. Fill the "Replace with" entry with "," or ", " (depending on preference).
  5. Un-check the "Match whole word" checkbox (the important bit that eludes logic).
  6. Check the "Extended" radio button.
  7. Click the "Replace all" button.

These steps turn e.g.

foo bar

bar baz

baz foo

into:

foo bar,bar baz,baz foo

or: (depending on preference)

foo bar, bar baz, baz foo

Community
  • 1
  • 1
Fred Gandt
  • 3,691
  • 1
  • 29
  • 37