Remove empty lines in text using Visual Studio or VS Code

RegexVisual StudioVisual Studio-2010Visual Studio-CodeLine

Regex 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

enter image description here

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.

Visual Studio 2013 replace empty lines

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:

  1. Ctrl + H (opens find and replace)
  2. Select use regular expression (Alt + E)
  3. Enter \n\r into the "Find..." input
  4. 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

  1. Open the replace box: Ctrl + H
  2. Turn on regular expressions: Alt + E
  3. Find: ^\s*$\n
  4. Replace: (leave empty)
  5. Replace All: Alt + A
  6. Turn off regular expression: Alt + E
  7. Hide the replace box: Esc
  8. Format the document: Ctrl + K and then Ctrl + D
  9. Replace the keyboard if you used to hit too hard

Solution 10 - Regex

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.

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
QuestionAlaaView Question on Stackoverflow
Solution 1 - RegexLennartView Answer on Stackoverflow
Solution 2 - RegexAlaaView Answer on Stackoverflow
Solution 3 - RegexAnas AlweishView Answer on Stackoverflow
Solution 4 - RegexsurfmuggleView Answer on Stackoverflow
Solution 5 - RegexJoe JohnstonView Answer on Stackoverflow
Solution 6 - Regextony09ukView Answer on Stackoverflow
Solution 7 - RegexpbzView Answer on Stackoverflow
Solution 8 - RegexxmedekoView Answer on Stackoverflow
Solution 9 - RegexShadi NamroutiView Answer on Stackoverflow
Solution 10 - RegexolegansoftView Answer on Stackoverflow
Solution 11 - Regexcmsmith81View Answer on Stackoverflow
Solution 12 - RegexGeno CarmanView Answer on Stackoverflow
Solution 13 - RegexMarcus CallenderView Answer on Stackoverflow