ChatGPT解决这个技术问题 Extra ChatGPT

Removing empty lines in Notepad++

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.


S
Stevoisiak

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)

https://i.stack.imgur.com/XelUW.png


Unfortunately 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.
If 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...
I was using this (even assigned my shortcut), but it's a lot slower than option above, so use smartly...
B
Brad

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.


Should first select 'extended` in search mode.
To get your lines not to be joined together you have to search for \r\n\r\n and replace with \r\n.
See @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.
If 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
@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
k
kolufild

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

Not 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 Unix sort -u on a Windows box without cygwin or unxutils or similar.
Here'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 by Plugins
will this work on all open documents, or one document at a time?
S
Sameer

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.


I had to use this method instead of some of the other methods to get it to easily work in a macro. Thank you.
even shorter and also lesser replacements needed because of multiple empty lines will be replaced at once: ^\s*$\r?\n
In notepad++ i used (^[ \t]*$\s)\s* in the find box and left replace empty. Works great. Thanks!
why does ^\s*$ work for multiple lines? shouldn't ^$ "restrict" it to one line? (@njank or anyone else)
Thanks for this regex expression, it worked well, but it took a while to complete execution.
A
Alex.K.

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.

https://i.stack.imgur.com/6cGfi.jpg


This worked for me without any issues or additional settings to change. Thanks!
Except that it's shorter, what exactly is the difference to SleepyBoBos's answer?
@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).
@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?
S
Saikat

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

https://i.stack.imgur.com/VRaoT.png

NOTE: for *nix files just find by \n


works well (note: not for lines that includes any non-print characters like space or tab)
Loved it! This should have been the accepted answer.
K
Kamal Nayan

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, select Regex Click on Replace All Done!


This is the only solution that did not screw up my huge .csv files. Thanks!
plus one because I finally know the shortcut for replace :-D
B
Bassem

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!
This assumes the line endings are Windows (CRLF) though, if your file is saved with Unix line endings search only for \n.
@Brad: I wrote in between the () "short for CR LF", didn't want codetrek to think \n is CR, hence the edit.
Wrong answer, puts all lines in one line.
A
Ahmad

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


P
Praboda

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

https://i.stack.imgur.com/k6v1q.png


Please note: This does not only replace empty lines, it replaces e.g. indents (tab and space) as well
Y
Ya Basha

You can search for the following regex: ^(?:[\t ]*(?:\r?\n|\r))+ and replace it with empty field


m
mahesh adepu

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!!
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.
P
Poisson

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.


a
andr

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


W
Wasim A.

https://i.stack.imgur.com/UxNET.png

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...


佚名

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


A
AshBrad

/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.


u
user2056154

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.
s
slyfox1186

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
_____________________________________

C
Chris

A few of the above expressions and extended expressions did not work for me, but the regular expression "$\n$" did.


p
poisson

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.


J
Jeremy W

this work for me:

SEARCH:^\r  
REPLACE:            (empty)

This will not work for linux eol \n and windows eol \r\n