Remove empty lines in text using Visual Studio or VS Code
RegexVisual StudioVisual Studio-2010Visual Studio-CodeLineRegex Problem Overview
How to remove empty lines in Visual Studio?
Regex Solutions
Solution 1 - Regex
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][1] is the conversion sheet from MSDN.
[1]: http://msdn.microsoft.com/en-us/library/2k3te2cs%28v=vs.110%29.aspx "Here"
Solution 2 - Regex
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
^$\n
-
In 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 [tag: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
Solution 3 - Regex
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
Solution 4 - Regex
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.
Solution 5 - Regex
Tested in VS 2012 to allow for pure line feeds.
^\s*$\n
hth
Solution 6 - Regex
I'm using visual studio 2017, non of the above worked for me until I tried \n\r
Steps:
- Ctrl + H (opens find and replace)
- Select use regular expression (Alt + E)
- Enter
\n\r
into the "Find..." input - Press replace
Solution 7 - Regex
To remove two or more adjacent empty rows with VS2012 use this:
^(?([^\r\n])\s)*\r?$\r?\n^(?([^\r\n])\s)*\r?$\r?\n
Solution 8 - Regex
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.
Solution 9 - Regex
VS 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
- Replace the keyboard if you used to hit too hard
Solution 10 - Regex
To remove double lines: ^:b*\n:b*\n replace with: \n
Solution 11 - Regex
In VS 2012, the regex string to use to find and replace all blank lines is ^(?([^\r\n])\s)\r?\n
Solution 12 - Regex
in VS2019 I just used ^..$ since none of the other answer actually worked.
Solution 13 - Regex
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.