VS2010 Code Snippet Shortcut Not Showing - visual-studio-2010

I've created a code snippet in VS2010. It isn't showing as a shortcut when I start typing. I've called it propnch.
It is available when I use Ctrl-K, Ctrk-X but when I just start typing prop... it isn't showing as an option.
Have I missed some kind of setting somewhere?
I had screen shots, but I don't think SO lets you upload any.
Edit: Screen Shots
I can see my snippet with Ctrl-K, Ctrl-X (its gone grey when I ctrl-PrtScn to take the screenshot)
But It doesn't appear with the other snippet shortcuts.
The snippet code is here (taken from this tutorial) and is in the "Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets" folder.
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propnch</Title>
<Shortcut>propnch</Shortcut>
<Description>Code snippet for property and backing field and ensure
that it invokes INotifyPropertyChanigng and INotifyPropertyChanged</Description>
<Author>Abhishek</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[
private $type$ $field$;
public $type$ $property$
{
get
{
return $field$;
}
set
{
this.OnPropertyChanging("$property$");
$field$ = value;
this.OnPropertyChanged("$property$");
}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>

It turns out this is a design flaw for the xml editor in VS2010. In the C# editor, you just type the shortcut and press 'tab'. IN the xml editor, it requires two more keystrokes.
To quote from the documentation:
To insert snippets using the shortcut name
1. Position the cursor where you want to insert the XML snippet.
2. Type < in the editor pane.
3. Press ESC to close the IntelliSense complete word list.
4. Type the shortcut name of the snippet, and press TAB to invoke the XML snippet.

According to screenshots, you have ReSharper installed and it overrides VS's IntelliSense behavior. You can either turn off Resharper's overriding or just add right in it a new LiveTemplate. More details here:
http://www.jetbrains.com/resharper/webhelp/Templates__Applying_Templates__Inserting_Imported_Code_Snippets.html
In my case I just added a new ReSharper template:
private $type$ _$loweredProperty$;
public $type$ $property$
{
get { return _$loweredProperty$;}
set
{
if (_$loweredProperty$ == value) return;
_$loweredProperty$ = value;
OnPropertyChanged("$property$");
}
}
adn it works even better: you have to type only two words - type and property name. The backing field will appear with lowered first letter. You must set "$loweredProperty$" to non-editable macros and point it to $property$. That's just a couple of clicks in Template editor.

Too a sec to realize, but it's simple: you were missing the </CodeSnippets> at the end.

CTRL +K+ X
or
Right click on code page will show you the snippet
right click and start intellisense in Visualstudio

Go in Extensions and Updates of Visual Studio and then click on Online Tab and then in search type Bootstrap.
Install following Packs to enable intellisense
Bootstrap Bundle
Bootstrap Snippet Pack

If it is snippets for XML language it must be placed in next directory
C:\Users\%user%\Documents\Visual Studio 2015\Code Snippets\XML\My Xml Snippets\
For adding one of them to your document you must call snippet context menu by
ctrl+K, ctrl+X.
Your snippets will be in "My Xml Snippets"

This may come a little bit late but if you are debugging a program CTRL K + CTRL X will not work. Stop debugging your program and try it again. it worked for me in VS 2013 and without Resharper.

Related

Custom Code Snippet not expanding on tab - this and other snippets otherwise work fine

Found a solution: snippet shortcuts cannot have underscores in them, or they will... well, they just won't expand from intellisense.
I am on Visual Studio 2022
Snippets / Intellisense are otherwise working fine, if I start typing "for", I get the right suggestions, and if I select one and press tab, it expands fine.
I have created my first custom code snippet (just to remind me to flush stdin after using scanf_s). It has no bells or whistles, it's just to insert some text. It does not share a name with any other command, variable, snippet or anything in Intellisense (the name is just "myscanf_s"). I am trying it in an open .cpp file, which matches its declared language.
It works in two ways, and fails in just one:
A) I can choose "Snippet - Insert Snippet", find it under "My Code Snippets", and insert it just fine.
B) It shows up as a suggestion when I type.
C) But, when selecting it as a suggestion and pressing tab (or double-pressing tab, double-clicking, or ctrl-spacing), it just inserts the full shortcut name.
Why is my custom snippet working so differently?
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>myscanf_s</Title>
<Description>Basic help for scanf_s</Description>
<Shortcut>myscanf_s</Shortcut>
</Header>
<Snippet>
<Code Language="CPP">
<![CDATA[
//%d is 'Dec. int', %u is 'Unsigned dec. int.', %s is 'String, %e is 'float', %c is 'single Char'
scanf_s("TYPE_SPECIFIER", &VARIABLE, WIDTH_OF_INPUT);
getchar();
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>```

Is there a code snippet in Visual Studio community IDE? [duplicate]

In Visual Studio I can type e.g.
for TAB TAB
and a code snippet pops in.
Are there built-in code snippets for private, public, etc. methods as well?
ctor: Default constructor
prop: Property
propg: Read-only property
sim: static int main method
svm: static void main method
There's a good list here. And if you want to make your own, the Snippet Designer is very good.
Here are all the Visual C# code snippets for Visual Studio 2017
You can download the method snippets as a Visual Studio Extension.
It supports the following:
method (typical method)
vmethod (virtual method)
smethod (static method)
xmethod (extension method)
In Visual Studio, go to menu Tools → Extensions and Updates...
Observe the Extensions and Updates window
Enter "C# Methods Code Snippets" in the search field (upper right)
If you want to see the list of all available snippets:
Press Ctrl + K and then Ctrl + X
Below are the steps I used to create a custom snippet for Visual Studio 2010, but the steps work for Visual Studio 2008.
Create a new text file named method.snippet and paste the following:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>method</Title>
<Shortcut>method</Shortcut>
<Description>Code snippet for method</Description>
<Author>Kevin Hogg</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>methodname</ID>
<ToolTip>Method name</ToolTip>
<Function>MethodName()</Function>
<Default>MethodNamePlaceholder</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[public void $methodname$ ()
{
$end$
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Copy your file into the snippets folder in Windows Explorer:
Visual Studio 2010: C:\Program Files (x86)\Microsoft Visual Studio
10.0\VC#\Snippets\1033\Visual C#
Visual Studio 2008: C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC#\Snippets\1033\Visual C#
Once you save your file, the snippets are automatically loaded, so you can now open Visual Studio and type:
method<tab><tab>
*where <tab> is the Tab key on your keyboard.
You should now see the following created, with the MethodNamePlaceholder highlighted so you can change the name.
public void MethodNamePlaceholder()
{
}
Some of the snippets I use, also mentioned at MSDN, follows:
'#if Creates a #if directive and a #endif directive.
'#region Creates a #region directive and a #endregion directive.
~ Creates a destructor for the containing class.
attribute Creates a declaration for a class that derives from Attribute.
checked Creates a checked block.
class Creates a class declaration.
ctor Creates a constructor for the containing class.
cw Creates a call to WriteLine.
do Creates a do while loop.
else Creates an else block.
enum Creates an enum declaration.
equals Creates a method declaration that overrides the Equals method defined in the Object class.
exception Creates a declaration for a class that derives from an exception (Exception by default).
for Creates a for loop.
foreach Creates a foreach loop.
forr Creates a for loop that decrements the loop variable after each iteration.
if Creates an if block.
indexer Creates an indexer declaration.
interface Creates an interface declaration.
invoke Creates a block that safely invokes an event.
iterator Creates an iterator.
iterindex Creates a "named" iterator and indexer pair by using a nested class.
lock Creates a lock block.
mbox Creates a call to MessageBox.Show. You may have to add a reference to System.Windows.Forms.dll.
namespace Creates a namespace declaration.
prop Creates an auto-implemented property declaration.
propfull Creates a property declaration with get and set accessors.
propg Creates a read-only auto-implemented property with a private "set" accessor.
sim Creates a static int Main method declaration.
struct Creates a struct declaration.
svm Creates a static void Main method declaration.
switch Creates a switch block.
try Creates a try-catch block.
tryf Creates a try-finally block.
unchecked Creates an unchecked block.
unsafe Creates an unsafe block.
using Creates a using directive.
while Creates a while loop.
I made my own snippet for a method. The XML code for it is the following, and you can add it to a file called "my_method.snippet" (or whatever_you_want.snippet) in C:\Users\YOUR_USERNAME\Documents\Visual Studio 2012\Code Snippets\Visual C#\My Code Snippets (your path might be different because I use VS2012):
<CodeSnippet Format="1.0.0">
<Header>
<Title>method</Title>
<Shortcut>method</Shortcut>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>access_modifier</ID>
<Default>private</Default>
</Literal>
<Literal>
<ID>return_type</ID>
<Default>void</Default>
</Literal>
<Literal>
<ID>name</ID>
<Default>New_method</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[$access_modifier$ $return_type$ $name$ ()
{
$end$
}]]>
</Code>
</Snippet>
</CodeSnippet>
You can create customs snippets.
Like this:
http://www.mediafire.com/file/gz3tzjnydk5/meth.snippet
The code snippet for properties is:
propTABTAB

How can I find (and rehabilitate, if necessary) the Code Snippet I created?

I created a code snippet (the first of many, hopefully) using mainly this article as guidance.
It seemed to work. Here are the steps I took:
First, I created this file with the code snippet (HtmlTableRowWithTwoCells.snippet):
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
<CodeSnippet Format="1.0.0">
<!-- The Title of the snippet, this will be shown in the snippets manager. -->
<Title>Create a 2-Cell HTML Table Row</Title>
<!-- The description of the snippet. -->
<Description>Creates a 2-Cell Row to be added to an HtmlTable</Description>
<!-- The author of the snippet. -->
<Author>Warble P. McGorkle for Platypi R Us (Duckbills Unlimited)</Author>
<!-- The set of characters that must be keyed in to insert the snippet. -->
<Shortcut>row2</Shortcut>
<!-- The set of snippet types we're dealing with - either Expansion or -->
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<!-- Now we have the snippet itself. -->
<Snippet>
<Declarations>
<Literal>
<ID>RowName</ID>
<ToolTip>Enter the Row instance name</ToolTip>
<Default>RowName</Default>
</Literal>
<Literal>
<ID>Cell1Name</ID>
<ToolTip>Enter the name for Cell 1</ToolTip>
<Default>Cell1Name</Default>
</Literal>
<Literal>
<ID>Cell2Name</ID>
<ToolTip>Enter the name for Cell 2</ToolTip>
<Default>Cell2Name</Default>
</Literal>
</Declarations>
<!-- Sepecify the code language and the actual snippet content. -->
<Code Language="CSharp" Kind="any">
<![CDATA[
var $RowName$ = new HtmlTableRow();
var $Cell1Name$ = new HtmlTableCell();
var $Cell2Name$ = new HtmlTableCell();
$RowName$.Cells.Add($Cell1Name$);
$RowName$.Cells.Add($Cell2Name$);
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Then, I created this "manifest" file (.vscontent):
<?xml version="1.0" encoding="utf-8" ?>
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005" >
<Content>
<FileName>HtmlTableRowWithTwoCells.snippet</FileName>
<DisplayName>Inserts a 2-Cell HTML Table Row</DisplayName>
<Description>Inserts a 2-Cell Row to be added to an HtmlTable</Description>
<FileContentType>Code Snippet</FileContentType>
<ContentVersion>2.0</ContentVersion>
<Attributes>
<Attribute name="lang" value="csharp"/>
</Attributes>
</Content>
</VSContent>
I zipped those two files together, renamed the extension from .zip to .vsi, 2-clicked it, and installed it into the "My Code Snippets" folder here with these steps:
And it indicates the snippet was installed in a reasonable location:
Yet, when I attempt to add a Code Snippet in VS, the only categories I see are these (no "My Code Snippets"):
When I select Tools > Code Snippets Manager..., I can navigate to Visual C# > My Code Snippets, but it is empty.
When I use the Code Snippets Manager's "Import" button and navigate to the location of the snippet and attempt to add the snippet file, I get, "The snippet files chosen were not valid."
Why does it tell me it installed successfully, when it apparently didn't (or where is it hiding)? What flaming hoop did I neglect to catapult myself through?
Is the "weird" name of the "manifest" file possibly the problem? ".vscontent" seems odd to me, but that's what the article referenced above says to name it. Perhaps that was just on oversight, and it should really be [snippetName].vscontent?
UPDATE
Apparently, naming the "manifest" file *.vscontent" is not the problem. Maybe it's a problem, but it's not the problem, because I named it the same as the .snippets file (except for the extension), went through the installation process again, and got the same exact results: seeming success, actual demoralization.
BTW, by default, when choosing a category into which to place the snippet, the Code Snipeets Manager puts a checkbox in "Microsoft Visual Studio Tools for Applications 2005 > My Code Snippets". I had previously unticked that and ticked the topmost "My Code Snippets"; this time I retained the default selection, PLUS my preferred location/category PLUS "Visual C#"
But alas and anon, the only category of those that seems to dispaly via Ctrl+K, X in VS is C#, and the expected shortcut ("row2") does not appear in the snippet dropdown.
Here's an easier way (and, as a bonus, it works):
Download the Snippets Designer:
Write the code directly in Visual Studio:
var RowName = new HtmlTableRow();
var Cell1Name = new HtmlTableCell();
var Cell2Name = new HtmlTableCell();
RowName.Cells.Add(Cell1Name);
RowName.Cells.Add(Cell2Name);
Right-click and select "Export as Snippet"
This puts the code in the Snippet Designer. Here's what it looks like after setting a few properties, and electing which parts of the code would be replaceable:
This is quite easy - sure beats the "olde-fashioned" way I was trying (which would have been okay, had it worked).
All I had to do was right-click the elements of code I wanted to replace on adding a new snippet, set the snippet's shortcut and description properties in Solution Explorer, save it, and voila!
Now mashing Ctrl+K, X in VS shows the snippet in "My Code Snippets" along with its description and shortcut:
Selecting it adds the snippet with the replacement strings highlighted:

Visual Studio wrap selection in quotes?

Is there is a way to wrap a selected text block with quotes? In visual studio I have not found a extension or plugin I am just looking for a simple way to do it. Is there a way to add that functionality?
The "Surround with" option is available in Visual Studio also without ReSharper. It doesn't contain the option to wrap in quotes. But it's possible to extend the snippets with custom wrappers. Also with double quotes. To do that:
Click File and then click New, and choose a file type of XML.
On the File menu, click Save <XMLFileName>.
In the Save as box, select All Files (.).
In the File name box, enter a file name with the .snippet file name extension.
Click Save.
Add this code to the file.
Code
<CodeSnippet Format="1.1.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>doubleQuotes</Title>
<Author>Microsoft Corporation</Author>
<Shortcut>"</Shortcut>
<Description>Wrap in double quotes</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>selected</ID>
<ToolTip>content</ToolTip>
<Default>content</Default>
</Literal>
</Declarations>
<Code Language="CSharp">"$selected$"</Code>
</Snippet>
</CodeSnippet>
Save the file.
Open Tools -> Code Snippets Manager.
In Language section select "Visual C#".
Click Import and browse to the snippet you just created.
Check My Code Snippets and click Finish and then OK.
To use it: Select text -> right click -> select "Surround with..." -> My Code Snippets -> doubleQuotes
Alternatively: Select text -> hit Ctrl + K, S -> My Code Snippets -> doubleQuotes
I got the idea for this solution from this answer where the author shows how to wrap code in custom HTML tags.
This might be overkill, but ReSharper offers a utility called Surround With that offers a templated mechanism for surrounding blocks of text. It doesn't look like they have a template out of the box for quotes, but you should be able to easily create one:
Plugin Description: https://www.jetbrains.com/resharper/help/Templates__Applying_Templates__Surrounding_Code_Fragments_with_Templates.html
You can use the following command (C# language) with my Visual Commander extension to wrap a selected text block with quotes:
public class C : VisualCommanderExt.ICommand
{
public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
ts.Text = '"' + ts.Text + '"';
}
}
Windows: IDE Visual Studio Code
Select the text you want to wrap,
Hold on SHIFT key Press " key it will wrap a text with double quotes.
[ in Addition you can also wrap < and { ]

Autocorrect / insert textblock by hotkey

I want to insert a default block of text when hitting a hotkey or when entering a special string in Visual Studio 2010 (either way would be fine).
Is there a fast and easy way to achieve this?
(Preferably without the use of third party extensions. )
Background / further explanation:
I want to use a special trigger-word to insert a default doxygen-commentblock, like "///" or "'''" for XML-commentblocks.
Other than the XML-functionality my inserted text does not have to be intelligent, it would suffice to just insert a default textblock.
My suggested trigger-string would be "---" as it would not collide with any program language I know. My suggested hotkey would be Alt+V.
Thanks for the help
Janis
Just write your own Code Snippet:
Code snippets are small blocks of reusable code that can be inserted in a code file using a context menu command or a combination of hotkeys. They typically contain commonly-used code blocks such as try-finally or if-else blocks, but they can be used to insert entire classes or methods.
Snippets are simply XML files that contain instructions to the Code Editor. For instance, here's the one that VS 2012 supplies for the C# if snippet:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>if</Title>
<Shortcut>if</Shortcut>
<Description>Code snippet for if statement</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>expression</ID>
<ToolTip>Expression to evaluate</ToolTip>
<Default>true</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[if ($expression$)
{
$selected$ $end$
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
You add new snippets to the IDE using the Code Snippet Manager, available from the Tools menu item from the main menu.

Resources