Visual Studio Code - delete all blank lines - regex

What also works is this regex pattern:

^\s*$\n

Then CTRL+Enter to replace all lines.

Explanation of the above pattern:

-----------------------------------------------
|  ^ | beginning of string anchor             |
-----------------------------------------------
| \s | any whitespace character               |
-----------------------------------------------
| '*'| zero or more repetitions               |
-----------------------------------------------
|  $ | end of string anchor                   |
-----------------------------------------------
| \n | new line                               |
-----------------------------------------------

Visual Studio Code 1.13.0 Linux Lite:

  • Hit CTRL+H
  • Select "Use Regular Expression"
  • Find box: ^(\s)*$\n (enter many ending \n as you need)
  • Replace box: empty
  • Click replace all

Empty lines gone!


For those who might be interested - what worked for me in version 1.3.1 (and still works in 1.33.1) to delete blank lines I used ctrl+h (find and replace) alt+r (Use regular expression)

In find box then:

\n\n

In replace box:

\n

This should make two consecutive end of line signs into one.

edited:

If you need to replace more empty lines (more than two) at once, you can use following regular expression in find box:

\n+

If you need to replace also empty lines with whitespaces, then you need to use following regular expression in find box:

\n+\s*\n

VS code is using javascript regular expressions