Sub topic 9
Give chat the right context
This chapter assumes you have Copilot installed and signed in, and that you can open the Chat view and send a prompt. You should also know how to open a folder in VS Code.
Copilot Chat answers from what it can see. A vague prompt with no context gets a generic answer. A prompt that points to the right files gets an answer based on your code. This chapter shows how to control what chat sees.
What chat sees by default
When you send a prompt, Copilot Chat may include some context on its own. Typical sources are:
- The file open in the active editor.
- Any text you have selected in that file.
- The text of your prompt.
Defaults can change between VS Code versions and between modes. Check the context shown in the Chat input area before you send a prompt, and add or remove items as needed.
Attach context with # references
Type # in the chat input to see a list of context items you can attach. Each reference tells Copilot to read a specific thing and use it in its answer.
| Reference | What it attaches |
|---|---|
| `#file` | A file you choose from your workspace |
| `#selection` | The text currently selected in the editor |
| `#codebase` | Relevant code that Copilot searches for in your workspace |
The available items depend on your VS Code version, so the list you see may differ from this table.
Here is a prompt that attaches one file:
Explain what #file:utils.ts does and suggest one way to simplify it.Copilot reads utils.ts and bases its explanation on the real contents. Without the reference, it would have to guess which file you mean.
Use the right amount of context
More context is not always better. Each attached item takes space in the model's context window, which is the limit on how much text it can consider at once. Too much unrelated material can bury the part that matters.
Use these rules of thumb:
- Attach the one or two files the question is about.
- Select the exact lines when your question concerns a single function.
- Use
#codebaseonly when you do not know where the relevant code lives.
Drag and drop and the Add Context button
You do not have to type references. You can also add context with the mouse:
- Choose Add Context in the Chat view and pick an item from the list.
- Drag a file from the Explorer into the chat input.
- Right-click selected code in the editor and send it to chat from the Copilot submenu.
Each method adds the same kind of attachment as the # reference.
Compare a vague and a specific prompt
Consider two prompts about the same code.
A vague prompt:
Why is this broken?A specific prompt with context:
The function in #selection returns undefined when the list is empty. Check how #file:api.ts calls it and suggest a fix.The second prompt names the symptom, the code in question, and the caller. Copilot can answer without asking you follow-up questions.
Context that applies to every prompt
Some context should apply to every prompt in a project, such as your language, test framework, and style rules. Retyping these each time is slow. A repository custom instructions file solves this, and the next chapter covers it.
Common mistakes
- Attaching the wrong file. Several files can share a name, such as
index.ts. Check the full path in the attachment before you send. - Relying on a stale selection. If you click elsewhere in the editor,
#selectionmay refer to different text than you expect. - Expecting memory across chats. A new chat session starts without the files you attached in an earlier one. Attach them again.
- Overloading one prompt. If an answer drifts, start a new chat with fewer attachments.
Exercise
You are asking Copilot Chat why a function in parser.ts throws an error, and you know which function it is. Which context would you attach, and why not use #codebase?
Show answer
Select the function's lines (or attach #file:parser.ts) so Copilot reads exactly that code. #codebase makes Copilot search the whole workspace, which is useful only when you do not know where the relevant code is. Here it would add unrelated material and could bury the function you care about.
Exercise
You attached utils.ts in one chat session, then opened a new chat and asked a follow-up about it. The answer is generic. What is the likely cause and the fix?
Show answer
A new chat session does not keep attachments from the earlier one, so Copilot cannot see utils.ts. Attach the file again with #file:utils.ts or Add Context, then repeat the question.