You want GitHub Copilot to suggest your own custom code snippets in Visual Studio Code. By default, Copilot generates code from public sources and your current file context. It does not automatically read your snippet files. This article explains how to configure VS Code so Copilot sees your custom snippets and uses them in its suggestions. You will learn the exact settings and file formats required.
Key Takeaways: How to Make GitHub Copilot Use Your Custom Snippets
- VS Code settings.json > github.copilot.advanced: Controls whether Copilot indexes snippet files for context.
- File path to snippet .json or .code-snippets files: Must be inside the workspace folder for Copilot to read them.
- Prefix and body fields in snippet definitions: Define the trigger text and the exact code Copilot will suggest.
How GitHub Copilot Interacts With Custom Snippets
GitHub Copilot generates code suggestions based on the content of your active editor, open tabs, and the workspace structure. It does not have a dedicated snippet library. Custom code snippets in VS Code are stored in JSON files with a .code-snippets extension or in a global snippets.json file. Copilot can treat these files as additional context if they are open or referenced in the workspace. The key is to make the snippet file part of the context Copilot analyzes when you type.
VS Code stores snippets in two locations. User-level snippets are in the user data folder, usually %APPDATA%\Code\User\snippets on Windows or ~/.config/Code/User/snippets on Linux and macOS. Workspace-level snippets are stored in a .vscode folder inside the project root. Copilot can read both types, but it only reads the snippet file if the file is open in an editor tab or if the file is part of the workspace index.
By default, Copilot does not index every JSON file in the workspace. The Copilot extension uses a context window that includes the current file, recently opened files, and files that are symbolically related. To force Copilot to see your snippet file, you must keep the snippet file open in a tab while you code. Alternatively, you can embed snippet-like code directly in a frequently used file so Copilot learns the pattern.
Steps to Configure VS Code and Copilot for Custom Snippets
Method 1: Keep the Snippet File Open While Coding
This is the simplest method. Copilot uses the content of all open editors as context. If your snippet file is open, Copilot can suggest code from it.
- Create or locate your custom snippet file
Open VS Code and press Ctrl+Shift+P. Type Preferences: Configure User Snippets and select it. Choose an existing language or create a new global snippets file. VS Code creates a .code-snippets file in the appropriate folder. - Define a snippet with a clear prefix
In the snippet file, write a snippet with a unique prefix. For example:{ "Console Log Variable": { "prefix": "clv", "body": ["console.log('$1', $1);"], "description": "Log variable name and value" } }The prefix clv is what you will type to trigger the snippet.
- Open the snippet file in a tab
Click the snippet file in the Explorer or use Ctrl+P and type the filename. Keep this tab open while you work in other files. - Type the prefix in your target file
In a JavaScript file, type clv. Copilot will suggest the snippet body based on the open snippet file. Accept the suggestion with Tab or Enter.
Method 2: Add Snippet Content Directly in a Context File
If you do not want to keep the snippet file open, embed the snippet code in a file that is always part of your workspace. For example, add the snippet code as a comment or a function in a utility file that you import frequently.
- Create a utility file
In your project root, create a file named snippets.js or snippets.py. Add the snippet code as a function or a comment block. - Write the snippet pattern as a function
Example for JavaScript:// Snippet: clv // console.log('variableName', variableName); function snippetConsoleLogVar(name, value) { console.log(name, value); } - Keep the utility file open or import it
Open snippets.js in a tab or import it in your main file. Copilot will see the pattern and suggest it when you type clv.
Method 3: Use the Workspace Index to Include Snippet Files
Copilot indexes files in the workspace for code completion. You can increase the chance Copilot sees your snippet file by placing it in the root of the workspace and ensuring it is not excluded from indexing.
- Place the snippet file in the workspace root
Move your .code-snippets file to the root folder of your project. Do not put it inside a .vscode folder if you want maximum visibility. - Check files.exclude in settings.json
Open VS Code settings (Ctrl+,). Search for files.exclude. Make sure the pattern /code-snippets is not listed. If it is, remove it. - Restart VS Code
Close and reopen VS Code. Copilot reindexes the workspace. Open a code file and type the snippet prefix. Copilot should now suggest the snippet body.
Common Issues When Using Copilot With Custom Snippets
Copilot Does Not Suggest the Snippet
The most common cause is that the snippet file is not open or not indexed. Open the snippet file in a tab. If the suggestion still does not appear, check that the snippet prefix is short and unique. A prefix like log may conflict with built-in snippets or common code patterns. Change the prefix to something like mylog or clv. Also, verify that Copilot is enabled for the language you are using. The Copilot status icon in the status bar should show a green checkmark.
Copilot Suggests the Wrong Code From the Snippet
Copilot may mix parts of your snippet with other context. This happens when the snippet file contains multiple snippets that are similar. Keep each snippet focused on one pattern. Use distinct prefixes and descriptive comments. If Copilot still suggests incorrect code, close other files that contain similar code patterns to reduce noise in the context window.
Copilot Ignores the Snippet File Entirely
If Copilot never references the snippet file, the file may be excluded from indexing. Check VS Code settings for search.exclude and files.watcherExclude. Remove any patterns that match the snippet file path. Also, ensure the snippet file is not larger than 100 KB. Copilot may skip very large files. Break large snippet collections into smaller files.
Copilot Suggestions vs VS Code Built-in Snippets: Key Differences
| Item | Copilot Suggestions | VS Code Built-in Snippets |
|---|---|---|
| Trigger method | Typing any code or comment that matches context | Typing the exact prefix defined in the snippet file |
| Context source | Open files, workspace index, and public code | Only the snippet file content |
| Customization | Requires open snippet file or embedded pattern | Directly defined in .code-snippets or snippets.json |
| Speed of suggestion | May take a moment to analyze context | Instant when prefix is typed |
| Reliability | Inconsistent if context changes | Always triggers on exact prefix |
You can now configure GitHub Copilot to use your custom VS Code snippets by keeping the snippet file open, embedding patterns in utility files, or adjusting workspace indexing. Start by opening your existing snippet file in a tab while coding. For more control, create a utility file with your most-used patterns. If Copilot still does not suggest your snippets, use the VS Code built-in snippet system as a fallback because it triggers on exact prefixes every time.