Condividi tramite


Surround with a Code Snippet

Keyboard:  CTRL + K, CTRL + S
Menu:  Edit -> IntelliSense -> Surround With
Command:  Edit.SurroundWith
Versions:  2008,2010
Published:  4/21/2010
Code:  vstipEdit0052

 

Download the seriously cool Tip of the Day Extension to get the daily tips delivered to your Start Page!

 

This is one that even people who know about snippets tend to forget.  You can actually put a snippet AROUND existing code.  Assuming you have some code selected:

image

 

Just press CTRL + K, CTRL + S:

image

 

Then type the statement you want to surround the code with.  In this case, I'll use an "if" statement:

image

 

Hit your TAB key once and you get your result:

image

 

Now you can put in your condition and any additional logic you want.  Pretty cool stuff!

Comments

  • Anonymous
    April 21, 2010
    Did this feature get removed from Visual Basic 2010? I can find "Surround With ..." in Visual Basic 2008 and Visual C# 2010 but not Visual Basic 2010. If so, I am curious as to why? It is a feature that I used a fair bit for inserting regions into existing code. It would be nice if you updated the tip to indicate that it does not apply to Visual Basic (if that is the conclusion). Thanks.

  • Anonymous
    April 21, 2010
    All I get is "The key combination (CTRL + K, CTRL + S) is bound to command (Surround With...) which is not currently available." I guess this doesn't work for C++, either. :(

  • Anonymous
    April 22, 2010
    Why is there features like this one, and it's not included in VB?

  • Anonymous
    May 04, 2010
    All -- So this feature is not currently available outside of C#.  There are snippet capabilities in VB / C++ but this isn't one of them.  I hope we will see this feature elsewhere as the team works on the next version of Visual Studio. Z

  • Anonymous
    September 02, 2010
    The comment has been removed

  • Anonymous
    September 08, 2010
    Actually, as I understand it, there is a big push internally to have all languages get equalized.  Not sure on the specifics but hope to hear more later on.

  • Anonymous
    November 18, 2012
    The comment has been removed

  • Anonymous
    October 17, 2014
    The comment has been removed