How to remove empty lines in Visual Studio?
-
For Visual Studio CodeDeepu Reghunath– Deepu Reghunath2019-02-19 12:05:24 +00:00Commented Feb 19, 2019 at 12:05
-
3Is there an answer for this without using regular expression? Just by using a shortcut like CTRL-K-D which formats the document but forgets to remove double blank lines.Jan– Jan2019-04-05 10:43:55 +00:00Commented Apr 5, 2019 at 10:43
-
How can we do this automatically in vs code when we format document or as we type? similar to what code maid does on visual studio?Kepol– Kepol2022-07-27 07:34:38 +00:00Commented Jul 27, 2022 at 7:34
12 Answers
Since Visual Studio 2012 changed its regex syntax, the original answers by Ala translate into the following in VS 2012:
Remove single blank lines
Old:
^:b*$\n
New:
^(?([^\r\n])\s)*\r?$\r?\n
Visual Studio 2013 (thanks to BozoJoe and Joe Johnston):
^\s*$\n
Remove double blank lines
Old:
^:b*\n:b*\n
New:
^(?([^\r\n])\s)*\r?\n(?([^\r\n])\s)*\r?\n
Rolls right off your tongue.
Here is the conversion sheet from MSDN.
7 Comments
It's very useful especially if you want to arrange or compare codes, thanks to the people who answer this question, I've got the answer from here and would like to share it with Stackoverflow:
Visual Studio (Visual Studio Code) has the ability to delete empty lines in replace operation using regular expressions.
Click Ctrl-H (quick replace)
Tick "Use Regular Expressions"
In Find specify
^$\nIn Replace box delete everything.
Click "Replace All"
All empty lines will be deleted.
Regular expression for empty line consists of
Beginning of line ^
End of line $
Line break \n
Note that normally in Windows an end of line indicated by 2 characters crlf - Carriage Return (CR, ASCII 13, \r) Line Feed (LF, ASCII 10, \n).
A regex to remove blank lines that are/aren't really blank (i.e. they do/don't have spaces): ^:b*$\n
To remove double lines: ^:b*\n:b*\n replace with: \n
*** for Visual Studio 2013 and above:***
^\s*$\n
and for double lines:
^(?([^\r\n])\s)*\r?\n(?([^\r\n])\s)*\r?\n
See the regular expression syntax updates for VS2012 and above in @lennart's answer below
3 Comments
Using Visual Studio 2017 and above
in Current Document
use shortcut
- Open Tools > Options or press Alt + T + O
- Under Environment tab > Keyboard
- Search for "DeleteBlank" and select
Edit.DeleteBlankLines - Add a new shortcut for example Ctrl+D,Ctrl+E
- Assign > OK
select all text and hit the shortcut
3 Comments
Tested in VS 2012 to allow for pure line feeds.
^\s*$\n
hth
5 Comments
In Visual Studio 2013 (Version 12.0.20623.01) i removed empty lines with this regular expression ^\r\n In the screen you can see the matched lines indicated by the brown squares.

1 Comment
VS 2022 and 2019
- Open the replace box: Ctrl + H
- Turn on regular expressions: Alt + E
- Find:
^\s*$\n - Replace: (leave empty)
- Replace All: Alt + A
- Turn off regular expression: Alt + E
- Hide the replace box: Esc
- Format the document: Ctrl + K and then Ctrl + D
3 Comments
^\s$ is sufficient as this translates into, only whitespace characters between the beginning and the end of the line.To remove two or more adjacent empty rows with VS2012 use this:
^(?([^\r\n])\s)*\r?$\r?\n^(?([^\r\n])\s)*\r?$\r?\n
1 Comment
Install CodeMaid and hit Ctrl+M, Space to clean up the code. (It formats the code, like Format Document Ctrl+E, D, as well). You can clean up more files from Solution Explorer.
Comments
Ctrl + K, Ctrl+D auto formats the current document and that removes unnecessary space in your code. It helps keep your code readable if that what you were looking for.
