Get better answers by setting the context for GitHub Copilot Chat in Visual Studio

You can use GitHub Copilot Chat in Visual Studio to get coding information and support, such as syntax, programming concepts, context-specific code help, test cases, debugging, and more, without leaving the IDE. Copilot Chat is integrated into Visual Studio, which means it can understand what you're working on by gathering useful clues from what you have in the IDE.

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

  • Use slash commands to quickly specify common tasks like /explain to get code explanations.
  • Scope the chat to specific files using references.
  • Review the source used by Copilot to generate the answer.
  • Use different threads for each of your Copilot chats so you can maintain different contexts in each.

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


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

Use slash commands in Copilot Chat 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
/clear Clear the chat window.
Example: /clear
Yes No
/doc Add comments for specified or selected code.
- /doc DeleteBasketAsync method in BasketService.cs
- select desired code and enter /doc
Yes Yes
/explain Get code explanations.

- /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.
- /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.
- /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
Yes Yes

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

Reference: scope Copilot results to a particular file

You can ask your coding related questions in natural language and GitHub Copilot Chat will answer these in the context of the codebase open in Visual Studio. With references you can get more specific about the information you want Copilot to consider when answering your question. By selecting a specific context in your codebase, you're able to form better questions easily without having to write out or paste long pieces of information. Specifying the context also enables Copilot to provide you with more relevant answers.

Screenshot of references in Copilot Chat.

To easily reference a file, simply add a # symbol at the beginning of the file name. For example, if you have a file named BasketService.cs, refer to it in the chat as #BasketService.cs. Here are some examples of using references for context control:

Example Context used by Copilot to form the question
What is the purpose of #MyFile.cs: 66-72? Exact section of the file
Where are the tests in #BasketService.cs? BasketService.cs
/explain the AddItemToBasket method in #BasketService.cs AddItemToBasket method in BasketService.cs

Review the sources used by Copilot Chat

Copilot Chat displays the context it used after every result, so that you can tell what was taken into account when answering your question. When you ask a Copilot Chat a question and get a response in the chat window, an References dropdown appears below the response. The entries in the References dropdown list show you the context referenced by Copilot Chat to generate that response. This information can help you modify your question to get better and more relevant answers.

Screenshot of References used dropdown in Copilot Chat.

Organize: isolate chats with Copilot into threads

If you’re using Copilot Chat extensively to ask questions as you code, you can organize your conversations in a way that keeps them on-topic. Copilot Chat for Visual Studio now provides an easy way to start new conversations (threads) to keep them focused on the task at hand, and keep the context clear so the answers are based on relevant history.

You can start a new thread by selecting the Create new thread in the chat window.

Screenshot of Create new thread icon in Copilot Chat.

You can select between multiple ongoing threads to provide the right historical context for your question.

Screenshot of switching between ongoing threads in Copilot Chat.