Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Refactoring

Aindriu Mac Giolla Eoin
Aindriu Mac Giolla Eoin
2,056 Points

Is there a shortcut to remove extra line spaces (more than 1) in Visual Studio

In my code I often have more than 1 line space in between lines of code. Is there a keyboard shortcut to remove them?

I read the following but was unsure if it would ruin my code:

  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"

2 Answers

Steven Parker
Steven Parker
231,198 Points

It looks like it would work.

I use the VsVim plug-in so I would do this differently. But it seems like it should work, since the regular expression would only match lines that have no content. If you also want to remove lines that have nothing but whitespace, you could try this expression in the Find box instead :point_right: ^\s*$\n

If you're concerned about "ruining" a file, just make a copy of the file first using the explorer or command line. Then you can overwrite it with your copy in case a Control-Z doesn't undo everything as it should.