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.
Regex – Removing empty lines in Notepad++
notepadregex
Related Solutions
[\r\n]+
should work too
Update March, 26th 2012, release date of Notepad++ 6.0:
OMG, it actually does work now!!!
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 theEOL
won't work. You can manually copy theEOL
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 forreturn;$
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 regexpCtrl+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 toEdit -> EOL Conversion -> Convert to Unix Format
, and after the replacement switch it back andEdit -> EOL Conversion -> Convert to Windows Format
.
You can access capturing groups like this:
var myString = "something format_abc";
var myRegexp = /(?:^|\s)format_(.*?)(?:\s|$)/g;
var myRegexp = new RegExp("(?:^|\s)format_(.*?)(?:\s|$)", "g");
var match = myRegexp.exec(myString);
console.log(match[1]); // abc
And if there are multiple matches you can iterate over them:
var myString = "something format_abc";
var myRegexp = new RegExp("(?:^|\s)format_(.*?)(?:\s|$)", "g");
match = myRegexp.exec(myString);
while (match != null) {
// matched text: match[0]
// match start: match.index
// capturing group n: match[n]
console.log(match[0])
match = myRegexp.exec(myString);
}
Edit: 2019-09-10
As you can see the way to iterate over multiple matches was not very intuitive. This lead to the proposal of the String.prototype.matchAll
method. This new method is expected to ship in the ECMAScript 2020 specification. It gives us a clean API and solves multiple problems. It has been started to land on major browsers and JS engines as Chrome 73+ / Node 12+ and Firefox 67+.
The method returns an iterator and is used as follows:
const string = "something format_abc";
const regexp = /(?:^|\s)format_(.*?)(?:\s|$)/g;
const matches = string.matchAll(regexp);
for (const match of matches) {
console.log(match);
console.log(match.index)
}
As it returns an iterator, we can say it's lazy, this is useful when handling particularly large numbers of capturing groups, or very large strings. But if you need, the result can be easily transformed into an Array by using the spread syntax or the Array.from
method:
function getFirstGroup(regexp, str) {
const array = [...str.matchAll(regexp)];
return array.map(m => m[1]);
}
// or:
function getFirstGroup(regexp, str) {
return Array.from(str.matchAll(regexp), m => m[1]);
}
In the meantime, while this proposal gets more wide support, you can use the official shim package.
Also, the internal workings of the method are simple. An equivalent implementation using a generator function would be as follows:
function* matchAll(str, regexp) {
const flags = regexp.global ? regexp.flags : regexp.flags + "g";
const re = new RegExp(regexp, flags);
let match;
while (match = re.exec(str)) {
yield match;
}
}
A copy of the original regexp is created; this is to avoid side-effects due to the mutation of the lastIndex
property when going through the multple matches.
Also, we need to ensure the regexp has the global flag to avoid an infinite loop.
I'm also happy to see that even this StackOverflow question was referenced in the discussions of the proposal.
Related Topic
- Convert tabs to spaces in Notepad++
- Javascript – How to use a variable in a regular expression
- Json – How to reformat JSON in Notepad++
- Html – RegEx match open tags except XHTML self-contained tags
- Removing duplicate rows in Notepad++
- How to format XML in Notepad++
- Regex: Remove lines containing “help”, etc
- Html – How To Auto-Format / Indent XML/HTML in Notepad++
Best Answer
There is now a built-in way to do this as of version 6.5.2
Edit -> Line Operations -> Remove Empty Lines
orRemove Empty Lines (Containing Blank characters)