Visual Studio Code - delete all blank lines - regex
Asked Answered
L

17

170

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.

Lali answered 1/4, 2016 at 7:22 Comment(4)
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)Outflank
Thank you Marie. It seems we have to wait for a while.Lali
Find this in regex mode ^$\n and replace with blank will also work fine. Happy Coding !!!Trilobate
Easy step: https://mcmap.net/q/55179/-visual-studio-code-remove-blank-lines-from-codeVioloncellist
P
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

Prescind answered 5/10, 2016 at 18:57 Comment(7)
Along these lines, I use ^[\r\n]{3,}Xanthippe
It's working now. Thanks :) A lots of versions of VSC in between but that's alright. Replace \n\n with \n worked nice.Lali
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)Prescind
This answer does not take all cases, what if you have spaces in blank lineWhydah
If you have spaces or tabs in blank lines, change the first regex to this: \n\s*\nSlovenia
Great! To remove more than one consecutive empty lines I replaced \n+ with \n. To acocunt for whitespaces too replace (^\s*\n)+ with nothingIrinairis
I wonder if there's an extension that does the equivalent to Notepad2: select, Alt+R.Pernicious
H
145

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                               |
-----------------------------------------------
Hanschen answered 17/5, 2017 at 7:2 Comment(4)
Not sure but only this worked for me, compare to all other answers. Well explained as well. Thank you.Abey
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).Leafage
Ctrl+Enter now inserts line break instead of replacing all.Gusella
The $ is redundant so you can simplify it to ^\s*\n.Horeb
E
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!

Epithelium answered 10/6, 2017 at 4:53 Comment(0)
C
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!!

Caste answered 23/4, 2018 at 13:11 Comment(0)
J
14

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

see here

Jacobsen answered 31/8, 2017 at 6:31 Comment(0)
P
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.

Pabulum answered 11/4, 2016 at 7:14 Comment(1)
this helped me only after I replaced all ^\s*$ with empty, then I tried (\r?\n){2,}Pessa
B
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!

Bombacaceous answered 9/1, 2018 at 12:42 Comment(4)
Hi @Jun! Im using Sublime Text3 wich has solved a lot of problems, plus is lighter and has some nice features.Bandsman
what plugin do u use with Sublime Text3? I used to use Webstorm and it is built in.Puritanism
Html / CSS / JS PrettifyBandsman
A note about 'blankLine' → be sure to add your language to language ids that it will process. For example add "csharp", to process .csharp filesEpstein
H
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.

Helbonnas answered 14/10, 2016 at 14:27 Comment(0)
L
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,}
Luxembourg answered 16/11, 2018 at 18:59 Comment(0)
R
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.

Reiche answered 4/6, 2017 at 15:22 Comment(2)
Not found (on Manjaro Linux).Bandsman
That's because it's a Visual Studio extension not a Visual Studio Code extension.Shell
R
0

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

Recurvate answered 3/9, 2017 at 15:6 Comment(0)
H
0

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

find:    (?:\s*$(\r?\n)){2,}
replace: $1
Hayrack answered 21/10, 2019 at 19:51 Comment(0)
C
0

install extention "Remove Blank Lines" in vscode

Clinkstone answered 17/11, 2020 at 5:27 Comment(0)
C
0

Regex to find at least 2 empty lines

\n\s*\n\s*\n
Craner answered 2/2, 2021 at 13:5 Comment(0)
E
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

Epochal answered 1/3, 2021 at 15:53 Comment(0)
N
-1

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

Nodarse answered 8/1, 2018 at 6:44 Comment(1)
and remove every blank space in your document?Graziano
W
-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.

Weep answered 30/7, 2016 at 23:58 Comment(1)
Visual Studio 2015 is not Visual Studio Code.Unearthly

© 2022 - 2024 — McMap. All rights reserved.