Reputation: 1853
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 :))
It must be I am doing something wrong. I just can't figure out what is it. Anybody knows? Thanks.
Upvotes: 170
Views: 119822
Reputation: 601
Visual Studio Code 1.13.0 Linux Lite:
^(\s)*$\n
(enter many ending \n
as you need)Empty lines gone!
Upvotes: 50
Reputation: 101
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
Upvotes: 0
Reputation: 11
There is my version for cleaning empty lines with white space:
find: (?:\s*$(\r?\n)){2,}
replace: $1
Upvotes: 0
Reputation: 3065
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
Upvotes: 252
Reputation: 206
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,}
Upvotes: 3
Reputation: 1142
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!!
Upvotes: 21
Reputation: 1719
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!
Upvotes: 5
Reputation: 31
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.
Upvotes: 3
Reputation: 1461
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 |
-----------------------------------------------
Upvotes: 146
Reputation: 372
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.
Upvotes: 0
Reputation: 5
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.
Upvotes: -4
Reputation: 961
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.
Upvotes: 6