How can I replace empty lines in Notepad++? I tried a find and replace with the empty lines in the find, and nothing in the replace, but it did not work; it probably needs regex.
22 Answers
There is now a built-in way to do this as of version 6.5.2
Edit -> Line Operations -> Remove Empty Lines
or Remove Empty Lines (Containing Blank characters)
-
1Unfortunately this doesn't consider your selection, it will delete all the empty lines in the file even if you have selected only some of them. Oct 11, 2016 at 11:53
-
2If this is a one-time operation, you can extract the selected lines to the new notepad++ tab and perform that operation there. Copy-paste procedure for 5seconds. Any other workaround that cannot be accomplished in that time is a waste in my opinion. But if you are doing that every day 100 times...– creedOct 16, 2018 at 10:17
-
I was using this (even assigned my shortcut), but it's a lot slower than option above, so use smartly...– BetlistaJun 9, 2019 at 20:43
You need something like a regular expression.
You have to be in Extended
mode
If you want all the lines to end up on a single line use \r\n
. If you want to simply remove empty lines, use \n\r
as @Link originally suggested.
Replace either expression with nothing.
-
23
-
15To get your lines not to be joined together you have to search for
\r\n\r\n
and replace with\r\n
. Jul 13, 2012 at 14:48 -
1See @BoltClock's comment below. Also, if your file is saved with Unix line endings, you can convert it to Windows line endings in Edit > EOL Conversion > Windows Format before doing the find and replace. Aug 16, 2012 at 15:51
-
11If you wish to remove lines with only spaces and tabs or nothing try using regex replace to match
\r\n\W*\r\n
and replace with\r\n
– MysterApr 14, 2014 at 2:25 -
4@ST3 hmm what was I thinking, This one is much better:
(\r\n|\r|\n)(\s*(\r\n|\r|\n))+
and replace with\r\n
see it in action here: regex101.com/r/qD9dB1/1– MysterAug 17, 2015 at 23:58
There is a plugin that adds a menu entitled TextFX
. This menu, which houses a dizzying array of quick text editing options, gives a person the ability to make quick coding changes. In this menu, you can find selections such as Drop Quotes, Delete Blank Lines as well as Unwrap and Rewrap Text
Do the following:
TextFX > TextFX Edit > Delete Blank Lines
TextFX > TextFX Edit > Delete Surplus Blank Lines
-
7Not sure as of when, but it would seem that this plugin is installed by default now. Slightly off-topic, but one of the other incredibly useful features, under
TextFX > TextFX Tools
, is line sorting, optionally removing duplicates. Very handy way of doing a Unixsort -u
on a Windows box without cygwin or unxutils or similar. Oct 6, 2011 at 1:00 -
3Here's a link to the TextFx Plugin. I found it very useful. Use:
Settings > Import > plugin...
and find the dll file to install it. It becomes a menu item byPlugins
Nov 2, 2012 at 19:41 -
1
- notepad++
- Ctrl-H
- Select Regular Expression
- Enter
^[ \t]*$\r?\n
into find what, leave replace empty. This will match all lines starting with white space and ending with carriage return (in this case a windows crlf) - Click the Find Next button to see for yourself how it matches only empty lines.
-
1I had to use this method instead of some of the other methods to get it to easily work in a macro. Thank you. Oct 9, 2014 at 15:32
-
7even shorter and also lesser replacements needed because of multiple empty lines will be replaced at once: ^\s*$\r?\n– njankDec 16, 2017 at 20:00
-
In notepad++ i used
(^[ \t]*$\s)\s*
in the find box and left replace empty. Works great. Thanks! Jun 20, 2020 at 22:52 -
why does
^\s*$
work for multiple lines? shouldn't ^$ "restrict" it to one line? (@njank or anyone else)– AlbinDec 1, 2020 at 22:56 -
1@albin, this pattern is matching a (possibly blank) line followed by a (possible single carriage return and) newline. It's the combination of "more than one blank line" that gives it so much power. I used the suggestion from njank and specified Replace with \n -- this replaced multiple blank lines with a single blank line.– Lyle S.Jun 16 at 23:57
- Press ctrl + h (Shortcut for replace).
- In the Find what zone, type
^\R
( for exact empty lines) or^\h*\R
( for empty lines with blanks, only). - Leave the Replace with zone empty.
- Check the Wrap around option.
- Select the Regular expression search mode.
- Click on the Replace All button.
-
2This worked for me without any issues or additional settings to change. Thanks!– Bonez024Jul 30, 2019 at 13:27
-
Except that it's shorter, what exactly is the difference to SleepyBoBos's answer?– AlbinNov 30, 2020 at 14:18
-
@Albin, different regex: SleepyBoBos: ^[ \t]*$\r?\n This will match all lines starting with white space and ending with carriage return (in this case a windows crlf) My version: ^\R ( for exact empty lines) or ^\h*\R ( for empty lines with blanks, only). Nov 30, 2020 at 16:55
-
@Alex.K. that I understood, since it's already mentioned in the answers themselves. Sorry, should have been more specific: is there anything else in the white space but space and tab in
\h
? Also\r?\n
already includes Win and Unix line breaks, whats the advantage of using\R
? Are there "regular" use cases where you need it?– AlbinDec 1, 2020 at 22:40
You can follow the technique as shown in the following screenshot:
- Find what:
^\r\n
- Replace with:
keep this empty
- Search Mode:
Regular expression
- Wrap around: selected
NOTE: for *nix files just find by \n
-
works well (note: not for lines that includes any non-print characters like space or tab)– AlbinNov 30, 2020 at 14:37
-
This worked for me:
- Press
ctrl + h
(Shortcut for replace) - Write one of the following regex in
find what
box.[\n\r]+$
or^[\n\r]+
- Leave
Replace with
box blank - In
Search Mode
, selectRegex
- Click on
Replace All
Done!
-
This is the only solution that did not screw up my huge .csv files. Thanks!– veuncentAug 30, 2017 at 14:30
-
1
1) Ctrl + H
( Or Search 🠆 Replace..
) to open Replace window.
2) Select 'Search Mode'
'Regular expression'
3) In 'Find What' type ^(\s*)(.*)(\s*)$
& in 'Replace With' type \2
^
- Matches start of line character(\s*)
- Matches empty space characters(.*)
- Matches any characters(\s*)
- Matches empty spaces characters$
- Matches end of line character\2
- Denotes the matching contend of the 2nd bracket
Refer https://www.rexegg.com/regex-quickstart.html for more on regex.
-
1Please note: This does not only replace empty lines, it replaces e.g. indents (tab and space) as well– AlbinNov 30, 2020 at 14:23
In notepad++ press CTRL+H , in search mode click on the "Extended (\n, \r, \t ...)" radio button then type in the "Find what" box: \r\n (short for CR LF) and leave the "Replace with" box empty..
Finally hit replace all
-
actually, I tried it, see my clarification in my answer. You were on to something!– BradOct 5, 2010 at 17:20
-
3This assumes the line endings are Windows (CRLF) though, if your file is saved with Unix line endings search only for
\n
. Oct 5, 2010 at 17:35 -
@Brad: I wrote in between the () "short for CR LF", didn't want codetrek to think \n is CR, hence the edit.– BassemOct 5, 2010 at 17:37
-
Well I'm not sure about the regex or your situation..
How about CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines and viola all blank line gone.
A side note - if the line is blank i.e. does not contain spaces, this will work
You can search for the following regex: ^(?:[\t ]*(?:\r?\n|\r))+
and replace it with empty field
Ctrl+H.
find - \r\r replace with - \r.
-
This works for me, removing extra line, in the sense of finding double \r replace with single \r. Thanks!!– genpetJun 3, 2012 at 1:59
-
This worked well, however I found that I needed to run it several times in my document before all instances of \r\r were gone. I'm unsure why.– HawkwingAug 21, 2013 at 16:59
This obviously does not work if the blank lines contain tabs or blanks. Many web pages (e.g. http://www.guardian.co.uk/) contain these white lines, as a result of a faulty HTML editor.
Remove white space using regular expression as follows:
change pattern: [\t ]+$ into nothing.
where [\t ] matches either tab or space. '+' matches one or more occurrences, and '$' marks the end of line.
Then use notepad++/textFX to remove single or extra empty lines. Be sure that these blank lines are not significant in the given context.
Sometimes \n\r etc not work, here to figure it out, what your actually regular expression should be.
Advantage of this trick: If you want to replace in multiple file at once, you must need this method. Above will not work...
- Edit >> Blank Operations >> Trim Leading and Trailing Spaces (to remove black tabs and spaces in empty lines)
- Ctrl + H to get replace window and replace pattern:
^\r\n
with nothing (select regular expression)
Note: step 1 will remove your code intendation done via tabs and blank spaces
CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines as suggested above works.
But if lines contains some space, then move the cursor to that line and do a CTRL + H. The "Find what:" sec will show the blank space and in the "Replace with" section, leave it blank. Now all the spaces are removed and now try CTRL+A, Select the TextFX menu -> TextFX Edit -> Delete Blank Lines
/n/r
assumes a specific type of line break. To target any blank line you could also use:
^$
This says - any line that begins and then ends with nothing between. This is more of a catch-all. Replace with the same empty string.
I did not see the combined one as answer, so search for ^\s+$ and replace by {nothing}
^\s+$ means
^ start of line
\s+ Matches minimum one whitespace character (spaces, tabs, line breaks)
$ until end of line
-
if you want multiple lines you can try:
^\s+^(\S)
and replace with\1
. Nov 3, 2017 at 5:57
This pattern is tested in Notepad++ v8.1.1
It replaces all spaces/tabs/blank lines
before and after each row of text.
It shouldn't mess with anything in the middle of the text.
Find: ^(\s|\t)+|(\s|\t)+$
Replace: leave this blank
Before:
_____________________________________
\tWORD\r\n
\r\n
\tWORD\s\tWORD\s\t\r\n
\r\n
\r\n
WORD\s\s\tWORD\t\sWORD\s\r\n
\t\r\n
\s\s\s\r\n
WORD\s\sWORD\s\s\t\r\n
____________________________________
After:
_____________________________________
WORD\r\n
WORD\s\tWORD\r\n
WORD\s\s\tWORD\t\sWORD\r\n
WORD\s\sWORD
_____________________________________
A few of the above expressions and extended expressions did not work for me, but the regular expression "$\n$" did.
An easy alternative for removing white space from empty lines:
- TextFX>TextFX Edit> Trim Trailing Spaces
This will remove all trailing spaces, including trailing spaces in blank lines. Make sure, no trailing spaces are significant.
this work for me:
SEARCH:^\r
REPLACE: (empty)
-
1