17

Does anyone know how to delete a line using Find & Replace in Notepad++ ?

In my Find query it finds the proper lines okay: ^.pPrev.$ In the Replace field, I leave it blank thinking the line should deleted (i.e. replaced with nothing), but the newline and endline characters remain.

6 Answers 6

17

Use the "Extended" setting in the Replace window (not "Regular expression": I'm sure there's a way to do it with Regular expression, but using "Extended" works fine).
Enter ".pPrev.\r\n" in the "Find what" field, and leave the "Replace what" field blank. This will include the \r\n characters in the match and delete the whole line.

4
  • 1
    Does not work anymore in later Notepad++ versions (5.9.2 UNICODE tested)
    – dtech
    Commented Jul 10, 2011 at 17:57
  • It seems like they keep pulling good features from Notepad++. First, the FunctionList plugin and now this. :(
    – Jim Fell
    Commented Oct 18, 2011 at 16:48
  • still working. If not, delete \n first and then delete \r
    – user4951
    Commented Jan 28, 2013 at 8:16
  • I came here looking for info on how to remove line breaks and \r\n worked for me like a charm. Thanks!
    – mabho
    Commented May 1, 2013 at 5:16
4

Using the "Regular expression search" option in the Find & Replace window:

Define the regex Find string that you are looking for, and include the \xd?\xa "end-of-line" characters in the match.

If want to delete these line brakes then leave the Replace string blank.

Note, the carriage return character is optional (\xd?), because you could have lines ending with no carriage return and only a line feed character \xa.

1
  • 1
    This worked for me where all other answers (pertaining to Notepad++) did not. I'm using v7.5.1
    – id.ot
    Commented Oct 26, 2017 at 17:13
1

The general principle whichever editor you're using is that you need to include the new line characters (\r\n assuming Windows line endings) in your search so that they will be included in the replacement and thus removed.

In Notepad++ this is made a bit more confusing by the 2 different search and replace commands. See this Wiki page for details: Replacing Newlines in Notepad++

I only had an old version of Notepad++ to hand so had to use Extended Search (accessed via ^R), making sure Regular Expr was unticked and using ^M to insert the new line character into the Find field. However if you make sure you're using Notepad++ 4.9 or later you should be able to use \r and \n in the regexp mode.

1
  • I'm not able to use \r or \n in RegEx mode (version 5.8.6). They only work in Extended mode for me.
    – Justin
    Commented Mar 21, 2011 at 18:00
1

You can actually select the newline by moving your cursor from the end of one line to the beginning of the next:

first line(from here)
(to here)decond line
third line

Then press ctrl+h while the new line is selected, and replace with nothing. This will delete all the newlines.

0

Not sure about Notepad++, but you can always do it with gVim. The command is like this:

%g/^.pPrev.$/d

You can download gVim here if you don't have it already.

0

Similar to what Wing said about 10 years ago.. I use the following to tidy up with a 'search and delete line' regex function in NP++

<search text>\r?\n?

In Wing's example: <search text>\xd?\xa

\x matches a hex code that corresponds to its ASCII counterpart

\xd (or \x0d) is used in Wing's example, instead of \r in my example

\xd means 'use hexadecimal d', which is 13 in decimal, and in ASCII '13' is a carriage return \r

Similarly for \xa (or \x0a), hexadecimal 'a' is 10 in decimal, which is a new line feed \n in ASCII.

See https://www.asciitable.com/ for all the ascii codes

The '?'s just make them optional

So use either of these in regex mode:

<search text>\r?\n?

or

<search text>\xd?\xa? (same thing as <search text>\x0d?\x0a?)

For example, I often strip unnecessary lines from online courses' table of contents:

Original text:

Dictionaries, Part I
checkmark circle
6m 25s
In NPP++

Regex mode ticked
Find what: checkmark circle\r?\n?
Replace with:
New text:

Dictionaries, Part I
6m 25s
1
  • Avoid posting answers to old questions that already got well received answers unless you have something substantial and new to add.
    – Toto
    Commented Apr 7, 2023 at 8:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .