Using Notepad++, how do I remove all lines starting with # or ;?
If you're at the end of a line and want to select the complete line, use the shortcut key combination Shift + Home . If there's only one line of text in the document, or you want to delete all text, press Ctrl + A key to select all text. Once highlighted, press delete to delete everything.
To begin with, if you want to delete a line containing the keyword, you would run sed as shown below. Similarly, you could run the sed command with option -n and negated p , (! p) command. To delete lines containing multiple keywords, for example to delete lines with the keyword green or lines with keyword violet.
The sed command can remove the lines of any range. For this, we just have to enter 'minimum' and 'maximum' line numbers. In this example, we will remove the lines ranging from 4 to 7 numbers. After removing these ranges of lines, our file will look like this.
Find:
^[#;].*
Replace with nothing. The ^
indicates the start of a line, the [#;]
is a character class to match either #
or ;
, and .*
matches anything else in the line.
In versions of Notepad++ before 6.0, you won't be able to actually remove the lines due to a limitation in its regex engine; the replacement results in blank lines for each line matched. In other words, this:
# foo ; bar statement;
Will turn into:
statement;
However, the replacement will work in Notepad++ 6.0 if you add \r
, \n
or \r\n
to the end of the pattern, depending on which line ending your file is using, resulting in:
statement;
As others have noted, in Notepad++ 6.0 and later, it is possible to use the "Replace" feature to delete all lines that begin with ";" or "#".
Tao provides a regular expression that serves as a starting point, but it does not account for white-space that may exist before the ";" or "#" character on a given line. For example, lines that begin with ";" or "#" but are "tabbed-in" will not be deleted when using Tao's regular expression, ^(#|;).*\r\n
.
Tao's regular expression does not account for the caveat mentioned in BoltClock's answer, either: variances in newline characters across systems.
An improvement is to use ^(\s)*(#|;).*(\r\n|\r|\n)?
, which accounts for leading white-space and the newline character variances. Also, the trailing ?
handles cases in which the last line of the file begins with #
or ;
, but does not end with a newline.
For the curious, it is possible to discern which type of newline character is used in a given document (and more than one type may be used): View -> Show Symbol -> Show End of Line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With