Just a learner
Just a learner

Reputation: 28572

How to share snippets between Visual Studio and Visual Studio Code?

I have some C# snippets in Visual Studio Code format below is an example (the file name of it is csharp.[Print to console].code-snippets:

{
    "Print to console": {
        "scope": "csharp",
        "prefix": "p",
        "body": [
            "Console.WriteLine(${10:\"${20:hi}\"});"
        ],
        "description": "Print to console"
    }
}

Visual Studio seems using a very different format for its snippets. An example is:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>cw</Title>
            <Shortcut>cw</Shortcut>
            <Description>Code snippet for Console.WriteLine</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal Editable="false">
                    <ID>SystemConsole</ID>
                    <Function>SimpleTypeName(global::System.Console)</Function>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[$SystemConsole$.WriteLine($end$);]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Is there a way for me to share snippets between them?

Upvotes: 1

Views: 1706

Answers (1)

dfrib
dfrib

Reputation: 73186

The CodeSnippetStudio by Alessandro Del Sole is an open source Visual Studio Extension allowing maintaining Visual Studio snippet files and exporting these as VS Code json snippets.

Code Snippet Studio is an extension for Visual Studio 2015 that makes it easy to create, edit, package, and share IntelliSense code snippets for Visual Studio 2015 and Visual Studio Code.

  • Create, edit, and save code snippets via convenient user interface and through a code editor that supports syntax-highlighting. You can save traditional .snippet files for Visual Studio 2015 and .json files for Visual Studio Code.

According to the project's User's Guide (docx), the tool will allow you to open/import both existing Visual Studio .snippet and VS Code .json snippets:

Opening Existing Code Snippets

You can open and edit existing .snippet and .json files by clicking ...

Code Snippet Studio will load the snippet file and will populate the user interface with the code, declarations, Imports, and References (where supported). So, you will be able to make further edits and to save back the code snippet.

Once opened, you should be able to export/save the snippet to your target of choice (json or snippet). This will allow you to share code snippets between Visual Studio and VS Code.


Note that there is an open issue to add support for Visual Studio 2017, where the author mentions that the CodeSnippetStudio should be usable also in Visual Studio 2017, just not for generating VSIX packages:

Hi guys, if you mean installing Studio on VS 2017, this is allowed.

If you mean generating packages for VS 2017, I would really love to add support.

The former should suffice if you are just maintaining your snippets for cross Visual Studio/VS Code support.

Upvotes: 2

Related Questions