Context
- 1 Understanding Context in GitHub Copilot
- 1.1 What is Context?
- 1.1.1 Adding Context
- 1.1.1.1 Add code files
- 1.1.1.2 Add context by selecting code
- 1.1.1 Adding Context
- 1.1 What is Context?
Understanding Context in GitHub Copilot
GitHub Copilot depends on the context it has access to. Context is the information Copilot uses to generate its suggestions, and the richer the context, the better the results.
What is Context?
The current file: Copilot reads the code above and below your cursor to understand what you’re working on.
Comments and function names: Well-written comments and descriptive names give Copilot strong clues.
Other files (limited): In most cases, Copilot can peek at a few other files in your open workspace, especially if they are recently opened or imported.
Adding Context
Add code files
There are multiple ways of adding files as context. The easiest way is to drag and drop files or folders into the chat window.
You can add additional context using the “Add Context…” button. This allows you to add any number of files.
If your question involves related files, such as a component’s CSS, tests, or utility modules, ensure those files are open in your editor so Copilot can consider them.
Add context by selecting code
Another way to guide Copilot is by highlighting the code you want help with and leveraging Copilot Chat. Highlight this function in your editor:
function formatDate(date: Date): string {
return date.toLocaleDateString();
}Open the Copilot Chat panel.
You will notice Copilot automatically sets lines 1–3 as its context.
It knows exactly what you’re looking at and that you may want help with this code.
Type / in the chat input.
This shows a list of built-in prompts like
/explain,/fix,/tests.
Choose /explain.
Copilot will respond with a plain-English explanation of what the highlighted code does.