Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create per workspace snippets in VSCode?

Tags:

I would like to add some project specific snippets.

How can I add snippets in the .vscode folder?

like image 794
Szabolcs Dombi Avatar asked Jun 01 '17 16:06

Szabolcs Dombi


People also ask

How do you make VS Code snippets?

Create your own snippets# To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages.

Can you have multiple workspaces in VS Code?

Untitled multi-root workspaces# You can start off by opening a folder in VS Code and then later add more folders as you see fit. Unless you already have opened a . code-workspace file, the first time you add a second folder to a workspace, VS Code will automatically create an "untitled" workspace.


2 Answers

Project Level Snippets were added in the September 2018 Release of VSCode (version 1.28):

Snippets can now be scoped to a project and shared with your team. Use the Preferences: Configure User Snippets command or create *.code-snippets file in the .vscode folder.

Project snippets work just like other snippets, they show up in IntelliSense and in the Insert Snippet action where they now have their own category.

Project Level Snippet

Snippets also now support multiple prefixes. If you cannot decide if your copyright header snippet should be prefixed as header, stub, or copyright, you can have them all. Use a string array as the prefix property.

{
  "prefix": ["header", "stub", "copyright"],
  "body": "Copyright. Foo Corp 2028",
  "description": "Adds copyright...",
  "scope": "javascript,typescript"
}
like image 60
Oliver Caine Avatar answered Sep 24 '22 11:09

Oliver Caine


Per this solution on vscode issues in github, you just need to:

  1. Create a file with .code-snippets extension (e.g. typescript.code-snippets), and
  2. Add the content you have in mind to that file. e.g. a simple fog snippet for console.log:
{
    "Print to console": {
        "prefix": "fog",
        "body": [
            "console.log('$1');",
            "$2"
        ],
        "description": "Log output to console"
    }
}
  1. Put that file in the .vscode folder of the workspace (but not inside subfolders of that).

It works like a charm on VS Code 1.36.1.

like image 45
Aidin Avatar answered Sep 24 '22 11:09

Aidin