Remove empty lines in text using Visual Studio.

Visual Studio has ability to delete empty lines in replace operation using regular expressions.
1.Click Ctrl-H (quick replace)
2. Tick "Use Regular Expressions"
3. In Find specify ^$\n
4. In Replace box delete everything.
5 Click "Replace All"
All empty lines will be deleted.

 Regular expression for empty line consist 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).

Addition from  Kenneth at 6/18/2009:
A regex to remove blank lines that are/aren't *really* blank (i.e. they do/don't have spaces):   ^:b*$\n

Addition from   Igor  at 8/5/2011:

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

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted @ Tuesday, March 04, 2008 4:28 PM
Print

Comments on this entry:

# re: Remove empty lines in text using Visual Studio.

Left by Rob at 5/15/2008 4:12 AM
Gravatar
Cool, thanks!

# re: Remove empty lines in text using Visual Studio.

Left by felipe at 5/30/2008 1:18 AM
Gravatar
Oh man i've been asking myself how to do this for years :D
thanks for the neat tip! this helped me a lot!!

# re: Remove empty lines in text using Visual Studio.

Left by Asbjorn at 7/31/2008 3:37 AM
Gravatar
Thanks alot :)

VS added 350.000 empty lines in one of my files..

# re: Remove empty lines in text using Visual Studio.

Left by Pete at 9/6/2008 4:34 AM
Gravatar
thanks! could you explain the regex? why is the newline character after the end of line marker?

# re: Remove empty lines in text using Visual Studio.

Left by Michael Freidgeim at 9/6/2008 2:42 PM
Gravatar
Pete,
I've added more info about regEx into my post.

# re: Remove empty lines in text using Visual Studio.

Left by Sung at 10/2/2008 9:53 AM
Gravatar
thanks for the tips.
after playing with it, i find \t\n with wildcards configuration do the works too.

# re: Remove empty lines in text using Visual Studio.

Left by Ross Youngblood at 10/20/2008 8:50 AM
Gravatar
This doesn't work in Visual Studio 6.0 (As far as I could tell.) Yes we are stuck in the dark ages.
I suppose I could download Emacs... and be done with this problem.

# re: Remove empty lines in text using Visual Studio.

Left by Michael Freidgeim at 10/20/2008 10:06 PM
Gravatar
Ross,
You also can download Visual Studio 2008 Express, which is a good editor (even if you have to compile in Visual Studio 6.0)

# re: Remove empty lines in text using Visual Studio.

Left by shophola at 4/27/2009 3:45 PM
Gravatar
cool! tips :D

# re: Remove empty lines in text using Visual Studio.

Left by Gyanendra Kumar Mishra at 5/18/2009 2:47 PM
Gravatar
Thanks really this code is a life saver

# re: Remove empty lines in text using Visual Studio.

Left by Kenneth at 6/18/2009 3:28 PM
Gravatar
I would offer an addendum:

A regex to remove blank lines that are/aren't *really* blank (i.e. they do/don't have spaces):

^:b*$\n

# re: Remove empty lines in text using Visual Studio.

Left by Steven Chalk at 8/23/2009 6:18 AM
Gravatar
simple when you see the answer but I wasted and hour or so trying combinations until I found this solution. Well done.

# re: Remove empty lines in text using Visual Studio.

Left by samy at 12/1/2009 11:49 AM
Gravatar
well. it is a lifesaver. i think the first line should be changed to ctrl+H instead of ctrl+D as mentioned

# re: Remove empty lines in text using Visual Studio.

Left by Michael Freidgeim at 12/2/2009 5:44 AM
Gravatar
samy, thanks, it's indeed ctrl-H

# re: Remove empty lines in text using Visual Studio.

Left by Haidar at 12/3/2009 6:55 AM
Gravatar
Great solution in short.

Thanks,
Haidar

# re: Remove empty lines in text using Visual Studio.

Left by Vaishnavi at 12/22/2009 2:54 AM
Gravatar
Thank you. helped me in formatting code which I got it from the net. Worked in a jiffy

# re: Remove empty lines in text using Visual Studio.

Left by Nouman Zakir at 3/23/2010 11:25 AM
Gravatar
thank you so much, i hate those blank lines when ever i paste some code from website or notepad. thanks to your tip i will never see those lines again.

# re: Remove empty lines in text using Visual Studio.

Left by Sagar at 3/31/2010 8:56 AM
Gravatar
ultimate..
it workss..!!
really thanks

# re: Remove empty lines in text using Visual Studio.

Left by mhd at 3/31/2010 11:08 PM
Gravatar
wow! why i didn't think of that before?!?
I think the 'magic' is in the $\n .
So,from now I will use $\n everytime I want to mass delete certain lines without leaving blank line trace. Thanks!!

# re: Remove empty lines in text using Visual Studio.

Left by danbo at 5/10/2010 4:22 PM
Gravatar
Kenneth, perfect for cleaning up the mess VS sometimes makes of the html.

thanks,

danbo

# re: Remove empty lines in text using Visual Studio.

Left by ELemus at 5/25/2010 2:20 PM
Gravatar
Gracias por la solución (Tanx 4 the solution) Great

# re: Remove empty lines in text using Visual Studio.

Left by Roman at 5/28/2010 8:09 AM
Gravatar
Thanks!!! 8))

# re: Remove empty lines in text using Visual Studio.

Left by Ed at 11/11/2010 1:55 PM
Gravatar
Thanks for this post. I found the resulting no lines a bit difficult to read.

I used

Search for: ^$\n+
Replace With:\n

# re: Remove empty lines in text using Visual Studio.

Left by mt at 1/25/2011 6:38 AM
Gravatar
Thanks A lot kenneth.

# re: Remove empty lines in text using Visual Studio.

Left by Anthony at 1/31/2011 3:48 PM
Gravatar
What about going the other way and inserting carriage returns?

tried replacing with \n or \r

predictably just adds '\n'

# re: Remove empty lines in text using Visual Studio.

Left by Anthony at 1/31/2011 3:51 PM
Gravatar
Just make sure regular expression check box in the options is checked, then it should work

# re: Remove empty lines in text using Visual Studio.

Left by EvilDr at 2/9/2011 2:53 AM
Gravatar
724 occurrence(s) replaced - in 0 seconds!
Dude that is one awesome tip :-D

# re: Remove empty lines in text using Visual Studio.

Left by Tim at 6/30/2011 10:41 AM
Gravatar
works perfectly thanks!

# re: Remove empty lines in text using Visual Studio.

Left by Igor at 8/5/2011 11:08 AM
Gravatar
Great, I modifyed your code to remove double lines:
^:b*\n:b*\n
replace with:
\n
Worked like a charm!

# re: Remove empty lines in text using Visual Studio.

Left by Srinu at 8/11/2011 11:33 PM
Gravatar
Thank u ...For this post...

# re: Remove empty lines in text using Visual Studio.

Left by logicalweb at 8/12/2011 5:23 AM
Gravatar
Thank u ...For this post...
even i've been asking myself how to do this for years :D

# re: Remove empty lines in text using Visual Studio.

Left by mice from space at 10/3/2011 6:05 PM
Gravatar
GREAT JOB!!!!

Your comment:



(not displayed)


 
 
 
 
 

Live Comment Preview:

 
«February»
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910