09-Best practices for using GitHub Copilot in VS Code
09-Best practices for using GitHub Copilot in VS Code
com/docs/copilot/prompt-crafting
(https://vscode.dev/github/microsoft/vscode-docs/blob/main/docs/copilot/prompt-crafting.md)
Note
"Prompt engineering" or "Prompt crafting" is a common phrase you'll hear when discussing AI and
refers to how and what information is packaged and sent to an AI API endpoint. The Copilot extension
does this process for you but you can help by providing hints to guide the extension.
If you are new to VS Code or GitHub Copilot, you might want to review the GitHub Copilot Overview (/
docs/copilot/overview) article first or dive straight into the Getting started (/docs/copilot/getting-started)
tutorial.
There are different options for optimizing your Copilot experience for inline suggestions and chat:
Open files
For code completions, Copilot looks at the current and open files in your editor to analyze the context and
create appropriate suggestions. Having related files open in VS Code while using Copilot helps set this
context and lets the Copilot see a bigger picture of your project.
Just as you would give a brief, high-level introduction to a coworker, a top level comment in the file you're
working in can help Copilot understand the overall context of the pieces you are creating.
It's best to manually set the includes or module references you need for your work. Copilot can make
suggestions, but you likely know best what dependencies you need to include. This can also help let
Copilot know what frameworks, libraries, and their versions you'd like it to use when crafting suggestions.
In the following TypeScript example, we want to log the output of the add method. When we don't have
any includes, Copilot suggests using console.log :
On the other hand, when you add a reference to Log4js , Copilot suggests using that framework for
logging the output:
Just as a method called fetchData() won't mean much to a coworker (or you after several months),
fetchData() won't help Copilot either. Using meaningful function names helps Copilot provide a body
that does what you want.
A function name can only be so descriptive without being overly long. Function comments can help fill in
details that Copilot might need to know.
One trick to get Copilot on the right page, is to copy and paste sample code that is close to what you are
looking for into your open editor. Providing a small example can help Copilot generate suggestions that
match the language and tasks you want to achieve. Once Copilot begins providing you with the code you
want and will actually use, you can delete the sample code from the file. This can be especially helpful to
jump start Copilot to a newer library version when it defaults to providing older code suggestions.
Always keeping a high quality bar can take discipline. Especially when you're coding fast and loose to get
something working, you might want to disable Copilot completions while in "hacking" mode. You can
temporarily disable completions from the Copilot status menu. Bring up the Copilot status menu dropdown
by selecting the Copilot Status bar item.
From the dropdown, you can disable completions entirely, or disable just for the active file type, for
example Markdown files.
You can also get assistance from Copilot via a chat interface (/docs/copilot/overview#_Answer-coding-
questions) by installing the GitHub Copilot Chat (https://marketplace.visualstudio.com/items?
itemName=GitHub.copilot-chat) extension.
When you're using chat to interact with GitHub Copilot, there are several things you can do to optimize
your experience.
Slash commands help Copilot Chat understand your intent when you ask a question. Are you learning
about a code base ( /explain ), do you want help with fixing an issue ( /fix ), or are you creating test
cases ( /tests )? By letting Copilot Chat know what you're trying to do, it can tune its reply to your task
and provide helpful commands, settings, and code snippets.
You could write out your project scope or current task with a natural language query but using chat
participants and slash commands is more concise and explicit.
For example, the #file variable lets you reference specific files from your workspace in your chat
prompt. This helps make the answers from Copilot Chat more relevant to your code by providing context
about the file you are working with. You can ask questions like "Can you suggest improvements to
#file:package.json?" or "How do I add an extension in #file:devcontainer.json?". By using the #file
variable, you can get more targeted and accurate responses from Copilot.
You can also add context to your chat message by using the Attach Context button in the Chat view. You
can then select the specific type of context from a Quick Pick, such as the current selection, one or more
files from the workspace, or one or more symbols from your source code.
Learn more about using context variables with Copilot Chat (/docs/copilot/copilot-chat#_add-context-to-
your-chat-prompt).
When you ask Copilot to do a specific task, be specific about the inputs, outputs, APIs, or frameworks you
want to use. The more specific your prompt is, the better the outcome will be. For example, instead of
"read product data from the database", use "read all products by category, return the data in JSON
format, and use the Mongoose library".
Here's an example using Inline Chat to create a function to calculate Fibonacci numbers:
You can even ask Copilot to follow coding conventions or improve variable names:
Even if you've already accepted a result, you can always ask Copilot to iterate on the code later.
If you'd like to learn more about productively using GitHub Copilot, you can follow up with these videos
and blog posts:
Yes No
02/06/2025
Follow @code(https://go.microsoft.com/fwlink/?LinkID=533687)
Request features(https://go.microsoft.com/fwlink/?LinkID=533482)
Report issues(https://www.github.com/Microsoft/vscode/issues)
Watch videos(https://www.youtube.com/channel/UCs5Y5_7XK8HLDX0SLNwkd3w)
(https://
www.microsoft.com)
(https://go.microsoft.com/fwlink/?LinkID=533687)
(https://github.com/microsoft/vscode) (https://www.youtube.com/@code)
Support (https://support.serviceshub.microsoft.com/supportforbusiness/create?sapId=d66407ed-3967-
b000-4cfb-2c318cad363d)
Privacy (https://go.microsoft.com/fwlink/?LinkId=521839)
Terms of Use (https://www.microsoft.com/legal/terms-of-use) License (/License)