168

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.

lepe
  • 24,677
  • 9
  • 99
  • 108
Joz
  • 1,833
  • 2
  • 12
  • 6
  • 1
    I added a comment on the issue you opened about this but for anybody else, `$` doesnt actually match the new line character, it matches a zero-width token that exists before the new line character. To replace a new line you need to use `\n` but VS Code doesn't currently support multi-line regex matches ([#313](https://github.com/Microsoft/vscode/issues/313)) – Marie Apr 06 '16 at 19:28
  • Thank you Marie. It seems we have to wait for a while. – Joz Apr 07 '16 at 05:36
  • 5
    Find this in regex mode ^$\n and replace with blank will also work fine. Happy Coding !!! – sajal rajabhoj Jun 28 '18 at 10:29
  • Easy step: **https://stackoverflow.com/a/50042582/6597375** – Deepu Reghunath Apr 24 '19 at 13:40

17 Answers17

252

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

suchoss
  • 3,022
  • 1
  • 19
  • 21
  • 13
    Along these lines, I use `^[\r\n]{3,}` – RJ Cuthbertson Jan 29 '17 at 04:44
  • It's working now. Thanks :) A lots of versions of VSC in between but that's alright. Replace \n\n with \n worked nice. – Joz Nov 18 '18 at 21:59
  • Also look at the @Dzumret solution below. In my solution it deletes only lines where there is nothing. His solution deletes lines where there is also some whitespace character (spaces, tabs) – suchoss Jan 23 '19 at 10:43
  • This answer does not take all cases, what if you have spaces in blank line – dhaker Feb 07 '19 at 12:43
  • If you have spaces or tabs in blank lines, change the first regex to this: `\n\s*\n` – AsGoodAsItGets Feb 14 '19 at 13:16
  • 2
    Great! To remove more than one consecutive empty lines I replaced \n+ with \n. To acocunt for whitespaces too replace (^\s*\n)+ with nothing – Ilario Feb 18 '19 at 11:06
  • I wonder if there's an extension that does the equivalent to Notepad2: select, Alt+R. – thdoan Nov 15 '19 at 22:20
144

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                               |
-----------------------------------------------
Asif Raza
  • 3,435
  • 2
  • 27
  • 43
Dzumret
  • 1,441
  • 1
  • 8
  • 3
  • 1
    Not sure but only this worked for me, compare to all other answers. Well explained as well. Thank you. – Sai May 07 '20 at 04:05
  • 1
    Placing the `\n` outside of the anchors is what worked for me. VSCode then allowed it to be replaced with nothing (blank "replace with" field). – Eric May 29 '20 at 20:12
  • Ctrl+Enter now inserts line break instead of replacing all. – AATHITH RAJENDRAN Aug 27 '21 at 08:35
50

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!

BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Antonio Delgado
  • 501
  • 4
  • 2
21

Here's my regex, it catches all extra new lines and empty lines that contain only space,tabs etc

\n\s*\n

And I replace all matches with \n

Explanation

\n       : New Line
\s*      : Zero or more consecutive white space characters or new lines
\n       : Another New Line

P.S :Remember to choose the regex option in the search window!!

Shabin Muhammed
  • 1,092
  • 2
  • 18
  • 29
13

Try using ^\s*\n in the Replace dialog of VS Code -

see here

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
VK.
  • 421
  • 5
  • 11
6

no, you're doing it right.

I get the same behaviour here.

I also tried another regex: (\r?\n){2,} but it seems that it only works for single lines.

maybe there is a preference to change the default regexp behaviour, or maybe VS is just built in such a way (line based)

ofcourse it's not a big deal to cut-paste and back from another text editor.

kobi7
  • 969
  • 1
  • 7
  • 15
5

I don't know about yout, but memorize a lot of commands for me seams a waste of time!

Use the extension "Blank Line Organizer", here's the description:

This extension will help you organize blank lines in the code by removing multiple blank lines. The extension removes blank lines only from the selected lines if any, otherwise from the entire file

How to use it: check the description of extension, but it really seams nice!

blankLine.triggerOnSave boolean true    If set to true, the command will be triggered on save.

In other words, after save the file, it automatically cleans up!

Marcelo Agimóvel
  • 1,668
  • 2
  • 20
  • 25
3

I found the following works best for me in Visual Studio:

Replace: ^\n$ With: <no value here>

This will find all empty lines and clear them out.

Asif Raza
  • 3,435
  • 2
  • 27
  • 43
Alan
  • 31
  • 1
3

At My case. kobi7 solution (\r?\n){2,} only worked for me, I had to run it again with small modification to make it work for single lines (just changed 2 to 1)

^(\r?\n){1,}
Mamdouh
  • 186
  • 2
  • 7
0

Code Maid Extension is all you need. You can use shortcut Ctrl M + Space bar to Cleanup your file, It will remove empty lines and format your code. You also can configure about format and cleanup rule. Hope this useful.

Nguyễn Top
  • 352
  • 2
  • 5
0

One or more line breaks (\n)+ and replace by \n

hector
  • 86
  • 1
  • 2
0

There is my version for cleaning empty lines with white space:

find:    (?:\s*$(\r?\n)){2,}
replace: $1
0

install extention "Remove Blank Lines" in vscode

0

Regex to find at least 2 empty lines

\n\s*\n\s*\n
Marc Thomann
  • 176
  • 1
  • 1
  • 7
0

First remove all blanks in empty lines Remove all blanks only Then remove all empty lines, empty lines is 2 or more line feeds Remove 2 or more occurrences of blank likes

jvergara
  • 101
  • 2
  • 4
-1

Replace: ^\n$ With: "blank space"

-4

Windows 10,Visual Studio 2015

Ctrl + H

Find... -> ^\s*

Replace all

Ctrl + A

Ctrl + K + F

Thank you for your question, I learned something new.