Try agent mode in VS Code!
Dismiss this update
By providing the right context, you can get more relevant and accurate responses from the AI in VS Code. In this article, you learn how to manage context in chat, including how to use #-mentions to reference files, folders, and symbols, how to reference web content, or how you can use custom instructions to guide the AI's responses.
VS Code automatically provides context to the chat prompt based on your current activity. The following information is implicitly included in the chat context:
The actual contents of the active file are not included in the context, but are shown as a suggested context item in the chat input box (indicated with italic text and a dotted outline). Depending on your prompt, VS Code might decide to read the contents of the active file. To explicitly include the contents of the active file in the chat context, select the suggested file in the chat input box.
In chat, you can explicitly refer to context by typing #
followed by the context item you want to mention. This enables the AI to provide more relevant responses based on the specific context you are referring to.
Type the #
symbol in the chat input field to see a list of available context items.
To reference a specific workspace file, folder, or code symbol, type #
followed by the file name, folder name, or symbol name. Learn more about referencing files and folders in chat.
Alternatively, choose from the list of available predefined context items like #changes
to get the diffs of changed files, or #codebase
to perform a codebase search for your workspace.
Make sure to enable the github.copilot.chat.codesearch.enabled (preview) setting to get the best results.
VS Code supports the following context items:
Context item | Description |
---|---|
#changes | Get the diffs of changed files in source control. |
#codebase | Perform a codebase search across the current workspace. |
#extensions | Search across VS Code extensions. |
#fetch | Get the contents of a web page. |
#githubRepo | Perform a web search within a GitHub repo. |
#problems | Get the list of problems for the current workspace. |
#selection | Get the text selection for the active editor. |
#searchResults | Get the results from the Search view. |
#terminalLastCommand | Get the last run terminal command and its status. |
#terminalSelection | Get the terminal text selection. |
#testFailure | Get the list of test failures. |
#usages | Get symbol references across the workspace. |
#vscodeAPI | Perform a search across the VS Code extension API. |
The following examples show how to use #-mentions in your chat prompts:
"Summarize the #changes"
"Generate release notes based on the #changes"
"Explain how authentication works in #codebase"
"Where is the database connecting string configured? #codebase"
"How do I build this #codebase?"
"Where is #getUser used? #usages"
"Create an about page and include it in the nav bar #codebase"
"Add a new API route for updating the address info #codebase"
"Add a login button and style it based on #styles.css"
"Fix the issues in #problems"
"Fix the failing tests #testFailure"
"What are the top #extensions for this workspace?"
"How do I use the 'useState' hook in react 18? #fetch https://18.react.dev/reference/react/useState#usage"
"Build an API endpoint to fetch address info, use the template from #githubRepo contoso/api-templates"
To let the AI automatically find relevant files and symbols in your workspace, you can use #codebase
. To provide specific files, folders, or symbols as context, add them to the chat using the following methods:
#-mention the file, folder, or symbol in your chat message by typing #
followed by the name of the file, folder, or symbol.
To reference a symbol, make sure to open the file containing the symbol in the editor first.
Drag and drop files or folders from the Explorer view, Search view, or editor tabs onto the Chat view to add them as context.
Use the Add Context button in the Chat view and select Files & Folders or Symbols.
If possible, the full contents of the file will be included when you attach a file. If that is too large to fit into the context window, an outline of the file will be included that includes functions and their descriptions without implementations. If the outline is also too large, then the file won't be part of the prompt.
Instead of adding individual files manually, you can let VS Code find the right files from your codebase automatically. This can be useful when you don't know which files are relevant to your question.
Add #codebase
in your prompt or select Add Context > Tools > codebase to enable code search for your workspace.
The following prompt examples show how to use codebase search:
"Explain how authentication works in #codebase"
"Where is the database connecting string configured? #codebase"
"Add a new API route for updating the address #codebase"
Make sure to enable the github.copilot.chat.codesearch.enabled (preview) setting to get the best results.
You can reference content from the web in your chat prompts, for example to get the latest API reference or code examples.
#fetch
: use this tool to retrieve content from a specific web page, such as a version-specific documentation page of a framework. To use this tool, type #fetch
followed by the URL of the page you want to reference.
"What are the highlights of VS Code 1.100 #fetch https://code.visualstudio.com/updates/v1_100"
"Update the asp.net app to .net 9 #fetch https://learn.microsoft.com/en-us/aspnet/core/migration/80-90"
#githubRepo
: use this tool to perform a code search within a GitHub repository, for example to reference code patterns or examples from another project. Type #githubRepo
followed by the repository name (for example, microsoft/vscode-docs
).
"How does routing work in next.js #githubRepo vercel/next.js"
"Perform a code review to validate it's consistent with #githubRepo microsoft/typescript"
"Add unit tests for my app. Use the same test setup and structure as #githubRepo rust-lang/rust"
Chat in VS Code has several built-in tools and you can further extend it with tools from MCP servers or extensions. For example, the #fetch
tool is a built-in tool that allows you to fetch content from a web page. You can also group tools into tool sets, which you can then reference in your chat prompts.
To reference a tool or tool set directly in your chat prompt, type #
followed by the tool (set) name and optional tool parameters. The following prompt examples show how to use tools:
Use the GitHub MCP server tool (configured as github-mcp
in your mcp.json
):
"what are my open issues #github-mcp"
"Implement a fix for issue #123 in contoso/tailwindtraders #github-mcp"
"What is PR 8407 in microsoft/vscode-docs about? #github-mcp"
Use the #postgres
MCP server to query a PostgreSQL database:
"Generate an API endpoint and data access layer for getting weather data from #postgres"
"What is the max length of the company name from #postgres"
When you use ask mode in chat, you can use the @
symbol to pass the chat prompt to a chat participant. A chat participant is a domain expert that has context and knowledge about a specific topic. For example, the built-in @vscode
participant is an expert on VS Code and the extension API, or @terminal
can help with shell commands.
To use a chat participant, start your prompt with @
followed by the participant name, and then continue with your question.
The following examples show how to use @-mentions in your chat prompts:
"@vscode how to enable word wrapping"
"@terminal what are the top 5 largest files in the current directory"
Type @
in the chat input field to see a list of available chat participants.
Extensions can also contribute their own chat participants.
VS Code has a built-in simple browser that you can use to view and interact with a locally-hosted web application, for example to do quick testing and debugging of your web application.
You can add elements from the Simple Browser window as context to your chat prompt. To do this:
Make sure to enable selecting elements from the Simple Browser with the chat.sendElementsToChat.enabled setting.
Run your web application locally.
Open the Simple Browser view by running the Simple Browser: Show command from the Command Palette.
Select the Start button to start selecting elements from the current page.
Hover over the elements of the web page and click to add them to the chat prompt.
Notice that the selected element is added as context to the current chat prompt.
You can configure which information is included in the context:
This functionality is also available in the Live Preview extension (pre-release).
Chat in VS Code is designed to be a multi-turn conversation. Within a chat session, VS Code uses the history of the conversation as context to your current prompt. This means that you can ask follow-up questions or clarify your previous question without having to repeat the context.
To start over with a new chat session and discard the current context, select the New Chat (+
) button (⌃L (Windows, Linux Ctrl+L)) in the Chat view. This can be useful if you want to move to a different topic and avoid the previous context and history.
Learn more about chat history and context management.
With instruction files, you can provide the AI with common guidelines and rules for generating responses that match your coding style and preferences. Instruction files are Markdown files that you can create in your workspace or in your current profile.
By using instruction files, you can avoid having to repeatedly add common instructions in your chat prompts, and instead have the AI automatically apply these instructions to your chat interactions.
Learn more about using instruction files.
VS Code uses an index to quickly and accurately search your codebase for relevant code snippets. This index can either be maintained by GitHub or stored locally on your machine.
The following workspace indexing options are available:
Learn more about workspace indexing.