You can define custom instructions in two ways:
- Using a
.github/copilot-instructions.md
file in your workspace - Directly in your VS Code settings through
settings.json
If you define custom instructions in both the .github/copilot-instructions.md
file and in settings, Copilot tries to combine instructions from both sources.
Custom instructions for code generation are not used for code completions and are only used in chat.
Custom instructions example
# Project coding standards ## TypeScript Guidelines - Use TypeScript for all new code - Follow functional programming principles where possible - Use interfaces for data structures and type definitions - Prefer immutable data (const, readonly) - Use optional chaining (?.) and nullish coalescing (??) operators ## React Guidelines - Use functional components with hooks - Follow the React hooks rules (no conditional hooks) - Use React.FC type for components with children - Keep components small and focused - Use CSS modules for component styling ## Naming Conventions - Use PascalCase for component names, interfaces, and type aliases - Use camelCase for variables, functions, and methods - Prefix private class members with underscore (_) - Use ALL_CAPS for constants ## Error Handling - Use try/catch blocks for async operations - Implement proper error boundaries in React components - Always log errors with contextual information
Use a .github/copilot-instructions.md file
You can store custom instructions in your workspace or repository in a .github/copilot-instructions.md
file and describe your coding practices, preferred technologies, and project requirements by using Markdown.
VS Code automatically includes the instructions from the .github/copilot-instructions.md
file to every chat request and applies them for generating code.
To use a .github/copilot-instructions.md
file:
- Set the github.copilot.chat.codeGeneration.useInstructionFiles setting to
true
to instruct Copilot in VS Code to use the custom instructions file. - Create a
.github/copilot-instructions.md
file at the root of your workspace. If needed, create a.github
directory first. - Add natural language instructions to the file. You can use the Markdown format.
Whitespace between instructions is ignored, so the instructions can be written as a single paragraph, each on a new line, or separated by blank lines for legibility.
.github/copilot-instructions.md
file. If you have a workspace that you use in both VS Code and Visual Studio, you can use the same file to define custom instructions for both editors.
Use settings
You can also configure custom code-generation instructions in your user or workspace settings. The following table lists the settings for each type of custom instruction.
Type of instruction | Setting name |
---|---|
Code generation | github.copilot.chat.codeGeneration.instructions |
Test generation | github.copilot.chat.testGeneration.instructions |
Code review | github.copilot.chat.reviewSelection.instructions |
Commit message generation | github.copilot.chat.commitMessageGeneration.instructions |
Pull request title and description generation | github.copilot.chat.pullRequestDescriptionGeneration.instructions |
You can define the custom instructions as text in the settings value or reference an external file in your workspace.
The following code snippet shows how to define a set of instructions in the settings.json
file. To define instruction directly in settings, configure the text
property. To reference an external file, configure the file
property.
"github.copilot.chat.codeGeneration.instructions": [ { "text": "Always add a comment: 'Generated by Copilot'." }, { "text": "In TypeScript always use underscore for private field names." }, { "file": "general.instructions.md" // import instructions from file `general.instructions.md` }, { "file": "db.instructions.md" // import instructions from file `db.instructions.md` } ],
Tips for defining custom instructions
- Keep your instructions short and self-contained. Each instruction should be a single, simple statement. If you need to provide multiple pieces of information, use multiple instructions.
- Don’t refer to external resources in the instructions, such as specific coding standards.
- Make it easy to share custom instructions with your team or across projects by storing your instructions in an external file. You can also version control the file to track changes over time.
- Split instructions into multiple files and add multiple file references to your settings. This approach is useful for organizing instructions by topic or type of task.
References
https://code.visualstudio.com/docs/copilot/copilot-customization