Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
216,108 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.