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