Edit

Share via


Customize chat responses and set context

You can ask GitHub Copilot Chat to give you code suggestions, explain code, generate unit tests, and suggest code fixes. Chat in Visual Studio can give you responses and generate code that matches your coding practices and project requirements, if you give it the right context. Instead of repeatedly adding this information in every chat prompt, you can store this context in files and automatically include it in every chat request.

In this article, you learn how to get better answers by providing more information to Copilot Chat:

Learn more about AI-assisted development in Visual Studio and how to use Copilot Chat in Visual Studio.

Prerequisites

To get started using GitHub Copilot Chat in Visual Studio, you need:

Support for GitHub Copilot Chat will be provided by GitHub and can be reached at https://support.github.com.

Prompting guidance

The guided chat experience in Visual Studio 17.12 and later helps refine your prompts to clarify context for better answers. GitHub Copilot Chat now guides you with clarifying questions when context is unclear.

Screenshot of guided chat experience with GitHub Copilot Chat.

Enable custom instructions

The custom instructions feature enables you to automatically add pre-specified contextual details to your chat questions. Copilot Chat uses these instructions tailored to your specific context, such as, the way your team works, the tools you use, or the specifics of your project, when generating responses.

To configure custom instructions:

  1. Create/add a custom instructions file .github/copilot-instructions.md in the root of your repository.
  2. Enable the feature in Visual Studio via Tools > Options > GitHub > Copilot > select (Preview) Enable custom instructions to be loaded from .github/copilot-instructions.md files and added to requests..

Custom instructions are not visible in the Chat view or inline chat. However, when used by Copilot, the.github/copilot-instructions.md file is listed in the References list of a response.

Use prompt files

You can now create reusable prompt files in your repository to run or share your frequently used prompts.

To create a prompt file:

  1. Write your prompts as you normally would in the prompt box, using # references to include specific information from your solution, such as methods, classes, and files. You can even reference other prompt files for shared instructions.
  2. Copy the prompt into a markdown file, and save it with the .prompt.md extension in the .github/prompts folder at the root of your repository.

To use a prompt file:

  1. Type #prompt: in chat input to reference any prompt files from your .github/prompts folder, or click the ➕ icon in the chat input to add it as context.
  2. Add any extra details, or simply enter the prompt as is.

Screenshot of using prompt files in GitHub Copilot Chat.

Use slash commands to set context for common tasks

Slash commands in Copilot Chat help you set the intent quickly for common development tasks. By using specific slash commands to form your question, you can get better answers without having to write out long questions.

You can use slash commands in a chat window, or directly inline in the code that you're looking to modify, using inline code assistance. Commands that help modify or add to the code file you have open in the editor will work both in the inline code assistant and the chat windows whereas commands for more general coding questions work only in the chat pane.

Command Usage Chat window Inline chat
/doc Add comments for specified or selected code.
Examples:
- /doc DeleteBasketAsync method in BasketService.cs
- select desired code and enter /doc
Yes Yes
/explain Get code explanations.

Examples:
- /explain the AddItemToBasket method in BasketService.cs
- select desired code and enter /explain
Yes Yes
/fix Propose a fix for problems in the selected code.
Examples:
- /fix the SetQuantities method in BasketService.cs
- select desired code and enter /fix
Yes Yes
/generate Generate code to answer specified question.
Example: /generate code to add two numbers in Calculator.cs
Yes Yes
/help Get help on using Copilot Chat.
Example: /help
Yes Yes
/optimize Analyze and improve running time of the selected code.
Examples:
- /optimize the AddItemToBasket method in BasketService.cs
- select desired code and enter /optimize
Yes Yes
/tests Create unit tests for the selected code.
Example: select desired code and enter /tests using XUnit Framework
Yes Yes

Screenshot of slash commands in inline chat view and chat windows.

With Visual Studio 2022 version 17.13, as the user types a slash command, the command expands out the prompt in natural language to display the context the command will use.

Next steps