Visual Studio Code - delete all blank lines - regex

RegexVisual Studio-CodeBlank Line

Regex Problem Overview


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.

Regex Solutions


Solution 1 - Regex

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

Solution 2 - 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                               |
-----------------------------------------------

Solution 3 - Regex

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!

Solution 4 - Regex

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!!

Solution 5 - Regex

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

see here

Solution 6 - Regex

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.

Solution 7 - Regex

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!

Solution 8 - Regex

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.

Solution 9 - Regex

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,}

Solution 10 - Regex

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.

Solution 11 - Regex

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

Solution 12 - Regex

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

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

Solution 13 - Regex

install extention "Remove Blank Lines" in vscode

Solution 14 - Regex

Regex to find at least 2 empty lines

\n\s*\n\s*\n

Solution 15 - Regex

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

Solution 16 - Regex

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

Solution 17 - Regex

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJozView Question on Stackoverflow
Solution 1 - RegexsuchossView Answer on Stackoverflow
Solution 2 - RegexDzumretView Answer on Stackoverflow
Solution 3 - RegexAntonio DelgadoView Answer on Stackoverflow
Solution 4 - RegexShabin MuhammedView Answer on Stackoverflow
Solution 5 - RegexVK.View Answer on Stackoverflow
Solution 6 - Regexkobi7View Answer on Stackoverflow
Solution 7 - RegexMarcelo AgimóvelView Answer on Stackoverflow
Solution 8 - RegexAlanView Answer on Stackoverflow
Solution 9 - RegexMamdouhView Answer on Stackoverflow
Solution 10 - RegexNguyễn TopView Answer on Stackoverflow
Solution 11 - RegexHector Matias GonzalezView Answer on Stackoverflow
Solution 12 - RegexArtur PaszekView Answer on Stackoverflow
Solution 13 - Regexmohammad ravandView Answer on Stackoverflow
Solution 14 - RegexMarc ThomannView Answer on Stackoverflow
Solution 15 - RegexjvergaraView Answer on Stackoverflow
Solution 16 - Regexuser3937422View Answer on Stackoverflow
Solution 17 - Regexthe name does not matterView Answer on Stackoverflow