Visual Studio Shortcut: Surround With

Posted by Jeff Widmer on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jeff Widmer
Published on Wed, 02 Feb 2011 21:30:45 GMT Indexed on 2011/02/02 23:26 UTC
Read the original article Hit count: 562

I learned a new Visual Studio keyboard shortcut today that is really awesome; the “Surround With” shortcut. 

You can trigger the Surround With context menu by pressing the Ctrl-K, Ctrl-S key combination when on a line of code.

Ctrl-K, Ctrl-S means to hold down the Control key and then press K and then while still holding down the Control key press S.

Here is where this comes in handy:

You type a line of code and then realize you need to put it within an if statement block. So you type “if” and hit tab twice to insert the if statement code snippet.  Then you highlight the previous line of code that you typed, and then either drag and drop it into the if-then block or cut and paste it.  That is not too bad but it is a lot of extra key clicks and mouse moves.

Now try the same with the Surround With keyboard shortcut.  Just highlight that line of code that you just typed and press Ctrl-K, Ctrl-S and choose the if statement code snippet, hit tab, and POW!... you are done!  No more code moving/indenting required.

Here is what the Surround With context menu looks like:

image

Just up or down arrow inside the drop down list to the code snippet that you want to surround your currently selected text with.  Did I mention this is AWESOME!

Now it is so simple to surround lines of code with an if-then block or a try-catch-finally block... things that usually took several key clicks and maybe one or two mouse moves.

And this works in both Visual Studio 2008 and Visual Studio 2010 which means it has been around for a long time and I never knew about it.

 

© ASP.net Weblogs or respective owner

Related posts about Visual Studio

Related posts about Tips & Tricks