185

How to remove empty lines in Visual Studio?

3
  • For Visual Studio Code Feb 19, 2019 at 12:05
  • 3
    Is 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
    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
    Jul 27, 2022 at 7:34

13 Answers 13

202

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
  • 1
    BozoJoe's answer, for me, worked well in VS2012 also (Update 4)
    – BertuPG
    Oct 28, 2014 at 12:18
  • 6
    ^\s+$\n changed to ^\s*$\n allow for (no content) pure line feeds. Jan 6, 2015 at 16:15
  • 4
    How can i make this automated, like working with CTRL+K+D?
    – ErTR
    Jun 10, 2016 at 9:05
  • Is this meant to work for strings or for code in the editor? Apr 6, 2017 at 19:35
  • @KyleDelaney Primarily for code in the editor. I don't know if the syntax is the same for strings (if you mean the Regex.* classes)
    – Lennart
    Apr 7, 2017 at 7:06
152

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 - 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
  • Works like a charm with cleaning XML file in Visual Studio 2015. Anyone know how to make a shortcut to execute this?
    – Leszek P
    Mar 4, 2016 at 10:51
  • 1
    Is there a way for this to work with the automatic formatter? Apr 6, 2017 at 19:38
  • 1
    ^\s*$\n worked fine for me. :) Feb 4, 2023 at 10:50
33

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

3
  • 1
    This only delete selected blank line(s) or from current line till the next non-blank line. It's not for the whole document or project.
    – Jan
    Apr 5, 2019 at 10:41
  • 3
    Try a select all before running the command, then it'll work. Oct 1, 2019 at 14:26
  • 1
    You should choose not used shortcut. I took Ctrl + Shift + Alt + D
    – backnext
    Feb 10, 2021 at 9:14
20

Tested in VS 2012 to allow for pure line feeds.

^\s*$\n 

hth

5
  • 2
    this works for me in VS 2015, the accepted answer did not unfortunately
    – EdwardM
    Apr 13, 2016 at 22:09
  • Works in VS 2013 as well. Mar 1, 2017 at 21:30
  • 2
    Works well in VS 2017, the accepted answer does not.
    – Germstorm
    Jun 8, 2018 at 10:52
  • 2
    Works in 2019 too Jun 15, 2020 at 16:47
  • Works in VS2022 Note: a trailing space was treated as part of the pattern. be sure to trim Jul 5, 2022 at 14:26
19

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

1
  • I also tried such way, but visual studio stuck frozen for some minutes and then replaced only 1 occurrence of several hundred I have! lol :-D
    – BertuPG
    Oct 28, 2014 at 12:12
10

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
10

VS 2022 and 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
3
  • also works in vs2022 :D Mar 18, 2022 at 16:33
  • Find: ^\s$ is sufficient as this translates into, only whitespace characters between the beginning and the end of the line.
    – fduff
    Jun 2, 2022 at 8:23
  • It works with Visual Studio 2022 Enterprise edition, I applied with 41.000 lines of code of DBContext. Nice answer!
    – Vy Do
    May 7, 2023 at 11:03
9

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
  • I was looking for a solution to replace two empty lines into just one. It works for me. Thanks! Sep 23, 2016 at 14:25
8

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.

0
4

To remove double lines: ^:b*\n:b*\n replace with: \n

Source: http://geekswithblogs.net/mnf/archive/2008/03/04/remove-empty-lines-in--text-using-visual-studio.aspx

1
  • 1
    This answer is a repeating to the same answers above! Apr 2, 2014 at 4:57
1

In VS 2012, the regex string to use to find and replace all blank lines is ^(?([^\r\n])\s)\r?\n

0

in VS2019 I just used ^..$ since none of the other answer actually worked.

-2

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.