Visual Studio Code - delete all blank lines - regex

Joz picture Joz · Apr 1, 2016 · Viewed 88.5k times · Source

I spent some time trying to figure out how to delete all blank lines in Visual Studio Code and I can't get it working. Anybody knows how to do it please?

If I search for ^$ while typing it in search field VSC does find the blank lines (completely blank lines, means no white spaces) but it doesn't remove them when I hit Replace All. It does nothing: ^$

For blank lines with spaces ^\s+$ Search works, but it does not remove them. What it does is it replaces them with a blank line without spaces :)) ^\s+$

It must be I am doing something wrong. I just can't figure out what is it. Anybody knows? Thanks.

Answer

suchoss picture suchoss · Oct 5, 2016

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