Guiding Copilot with context
1. Guiding Copilot with context
Welcome back to a new chapter in the course!2. The importance of context
Copilot's responses are only as good as the context you provide. The difference between average3. The importance of context
and exceptional results comes down to how well you manage what information Copilot can see.4. Adding files to Copilot's context
IDEs like Visual Studio Code automatically provide some context to Copilot without you doing anything. Such context includes your currently selected text or your active file. You can also drag files directly from Explorer onto the chat, or use the Add Context button to select specific files and folders. But to get the best results, you need to be intentional about the context you provide. One of the best ways to provide targeted context is to use chat variables. To use them, type a hash (#) in the chat, followed by a variable. Let's take a look at some of the most useful ones.5. The #codebase variable
The #codebase variable tells Copilot to perform a code search across your entire project. For example, you might say: "How is user authentication handled? #codebase" Copilot will scan your workspace and include relevant logic, function calls, or configuration settings in its answer.6. How Copilot understands your codebase
Behind the scenes, Copilot uses an index of your codebase to find relevant code snippets and context. There are two types of indexes: A local index, which is a semantic index stored on your machine, and a remote index, used for repositories hosted on GitHub.7. Checking your index type
You can check which one you're using8. Checking your index type
by opening the Copilot status dashboard from the Status Bar in your IDE.9. The #changes variable
The #changes variable references your most recent source control changes. For example, you could say: "Will any of these changes break the login flow? #changes" This chat variable is great when you're evaluating something you just modified.10. The #selection variable
If you want Copilot to focus on a specific block of code, you can highlight it in your editor and reference it using #selection. Then, you can ask things like: "Can you refactor this? #selection" This is especially handy for making quick edits and being targeted in your prompts.11. The #fetch variable
If you need Copilot to consider something outside your codebase, like documentation, an API reference, or even a blog post, you can use #fetch to pull in the contents of a web page. For example: "Summarize this documentation for me. #fetch https://example.com/docs" Copilot will fetch the page and use it as part of your chat context.12. The #files variable
You can also use the #files variable to attach one or more specific files to your prompt. For example, if you want Copilot to review a utility script, you could write: "Can you check this file for error handling issues? #files:utils/data_loader.py" Copilot will analyze the contents of that file—even if it's not currently open—and include it as part of your prompt context.13. Other chat variables and tools
You can explore many other chat variables, like #search, #testFailure, #problems, #terminalLastCommand, or #githubRepo, each designed to surface more context from your workspace or beyond. You can type # in the Copilot chat input box to view all available options.14. The chat history as context
When in a chat, Copilot remembers past interactions. So, your chat conversation history becomes part of the context as well. This means you can build complex solutions step by step, instead of overwhelming Copilot with one massive prompt.15. Building a progressive chat interaction
So, instead of saying: "Add PostgreSQL integration with authentication, persistence, sessions, error handling, and connection pooling." Try this progressive flow: "What's the best approach for adding database persistence? #codebase", "Implement the database connection using the suggested approach", "Help me with the schema design for the Task and the User classes", "Now add session management and basic error handling." Each message builds on the previous one, improving Copilot's accuracy and giving you more control over the development process.16. Starting a new chat to clear context
And if you ever want to start fresh, just start a new chat.17. Starting a new chat to clear context
This clears the current context!18. Copilot works better with context
With the proper context, GitHub Copilot becomes a more powerful coding assistant.19. Let's practice!
Now it's your turn to practice these concepts!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.