visual-studio-codecode-snippets

Where do code snippets reside in Visual Studio Code?


The only C/C++ extension I have is the official one from Microsoft. I do not have any other snippet/intellisense/autocompletion extension.

See below all the extensions loaded:

enter image description here

In trying to create shortcut keys/prefixes for my snippets, I would like to make sure that it does not clash with any pre-existing shortcuts/prefixes for other snippets. Is there a way to know / look through all currently available code snippets in VSCode and their shortcut keys/prefixes?

I tried to Insert Snippet via the command pallette in the hope that it would reveal all available snippets. Unfortunately, it does not list all snippets. See, for instance, below image, where this command pallette does not show the existence of a for snippet and yet inside the editor, when I type for, there is an option to add such a snippet.

enter image description here


Solution

  • Snippets defined by extensions are stored in JSON files with the extension's contents. Open the extension's directory in the .vscode/extensions/ directory in your user home directory, and then open its package.json file (extension manifest), look for the "snippets" property, which will point to the paths of its snippet files.

    Extensions can also provide snippets dynamically through the VS Code API. See also the SnippetString interface. Snippets can be provided by an extension in multiple ways, including through CompletionItems (suggestions), DocumentDropEdits, InlineCompletionItems, SnippetTextEdits, TextEdits, or TextEditor#insertSnippet. If you want to know about all those, this probably means reading source code for the extension. Some extensions even have a separation between frontend and backend, with closed-source backends (such as the Microsoft C++ extension).

    To open user-level snippets, you can just open the snippet file in VS Code (use Snippets: Configure User Snippets in the command palette), and the full path can be obtained by hovering the tab handle, or in the breadcrumbs, or by using File: Copy Path of Active File.

    Profiles can also have snippet files. You can find them in the snippets/ directory of the profile's storage (under .../Code/User/profiles, where ... is some platform specific path)

    You can also have workspace/project snippets (snippets that only apply for a specific workspace) in the .vscode/ directory of the workspace folder with filenames ending in .code-snippets.

    If you want to find out which extension contributes a specific snippet suggestion, then see How can I find out which extension contributes a specific suggestion in VS Code and whether it can be changed by a setting?.