Partager via


Did you know... You can insert a code snippet via its shortcut keyword - #105

Update: thanks to Bill for catching that VB doesn’t show snippet shortcuts in the statement completion.  I’ve updated the VB picture below to better illustrate what is going on.  Also, check out Bill’s post, as he talks about using the ‘?’ to insert snippets. 

This is probably a much better tip than the previous two.  Code Snippets have the support to be given a "shortcut", usually an abbreviated version of the code snippet name that you can type into the editor and hit tab to insert.

To insert, simply type in the name of the snippet, e.g. "for", then hit tab.  Note that if statement completion is open, you’ll have to hit tab twice to insert the snippet.

In both Visual Studio 2005 and 2008, you will be able to see C# Code Snippet shortcuts in the Statement Completion window.  Below is the for snippet displayed within the Statement Completion window.  Note the snippet icon to the left.

For Snippet In StatementCompletion

In Visual Studio 2008, you won’t see VB snippets in the statement completion window, but you will see a note in the tooltip when you can hit tab twice to insert the corresponding snippet.

VB For keyword in statement completion

Technorati tags: VS2005Tip, VS2008Tip

Comments

  • Anonymous
    December 11, 2007
    VB does NOT display snippets in the statement completion window, it only displays namespaces, types, members and Keywords. Also note that with VB you cannot insert a snippet by typing it's name and hitting tab if the shortcut is a substring of anything that displays in the completion window. (you could in 2005, but this was broken intentionally for 2008)

  • Anonymous
    December 11, 2007
    Sara Ford writes about inserting snippets in VB and C#. Unfortunately, Sara has got it quite right. In

  • Anonymous
    December 12, 2007
    All code snippets are found in the Code Snippet Manager. It is found at Tools - Code Snippet Manager

  • Anonymous
    January 14, 2008
    All code snippets are found in the Code Snippet Manager. It is found at Tools - Code Snippet Manager.