GitHub Copilot in VS Code
The GitHub Copilot extension is an AI pair programmer tool that helps you write code faster and smarter. You can use the Copilot extension in Visual Studio Code to generate code, learn from the code it generates, and even configure your editor.
With GitHub Copilot in VS Code you can:
- Get inline code suggestions while you're writing and iterating on code.
- Start a chat conversation to generate or refactor source code, produce documentation comments, or generate unit tests.
- Get help with fixing errors in your code, or resolve error while running commands in the terminal.
- Ask questions to help ramp-up on a new code base, or accelerate learning a new programming language or framework.
- Use chat features to discover and configure your VS Code setup.
To get started with GitHub Copilot in VS Code:
Sign up for a GitHub Copilot free trial in your personal account, or have a GitHub Copilot seat assigned by your organization.
Install the GitHub Copilot extension.
Follow the steps in the Getting started tutorial to get you set up and generate your first AI-powered code suggestions.
Next, learn how to use chat features to help with refactoring code and improving code understanding in the Copilot Chat tutorial.
Now that you've signed up for Copilot and activated the extension, let's see its assistance in action!
As you start writing code or code-related items (comments, test, and more), Copilot presents suggestions automatically in the editor to help you code more efficiently. For any given input, Copilot might offer multiple suggestions.
Notice in the following example how Copilot suggests an implementation of the
When you're presented with an inline suggestion, you can accept it with the Tab key. Optionally, you can hover over the suggestion to any of the other suggestions.
Rather than letting Copilot provide suggestions as you're typing, you can also use code comments to provide instructions to Copilot. By using code comments, you can be more specific the suggestions you're looking for. For example, you could specify a type of algorithm to use, or which methods and properties to add to a class.
The following example shows how to instruct Copilot to create a class in TypeScript to represent a student, providing information about methods and properties:
In addition to inline suggestions, you can also get assistance from Copilot via a chat interface. You can use Copilot Chat in VS Code in different ways, depending on the context:
- Chat view: Ask Copilot for help with any task or question in the Chat view, bringing in code from your current files.
- Inline Chat: Apply Copilot's AI suggestions directly to your code, staying in the flow.
- Quick Chat: Bring up a Chat-powered dropdown for quick questions and suggestions.
To use the chat features in VS Code, install the GitHub Copilot Chat extension.
When you're developing a project or learning something new, it can be a significant help to get AI assistance on your questions, large or small. Copilot enables an interactive Chat experience that understands the context of your code, workspace, extensions, settings, and more.
You can access the Chat view via the Activity Bar, or use the ⌃⌘I (Windows, Linux Ctrl+Alt+I) keybinding:
Copilot suggests potential questions to get started. You can select any of these questions, or use the chat input box to type your own. Notice in the following example that Copilot Chat understands the context of your workspace when asking about "my devcontainer.json" file:
As you continue asking questions, Copilot maintains the history of your conversation, and provides related follow-up questions or commands in its response too.
Tip: To make your interactions more efficient, you can use slash commands as a shorthand for common questions. For example, use the
/helpto get help about GitHub Copilot. Have you started working on a new code base, then try entering
/explainto let Copilot help you understand the code.
Depending on your question, Copilot Chat might return source code in a code block. Hovering over the code block presents options to Copy and Insert at Cursor (⌃Enter (Windows, Linux Ctrl+Enter)).
If Copilot Chat detects that a code block contains a command, you can run it directly in the integrated terminal with Insert into Terminal (⌃⌥Enter (Windows, Linux Ctrl+Alt+Enter)). This option creates or opens the active terminal and inserts the command text, ready for you to run.
Another key functionality of Copilot is answering questions inline as you're coding. This allows you to harness the power of AI while staying in your existing editor workflow.
In any file, you can press ⌘I (Windows, Linux Ctrl+I) on your keyboard to bring up Copilot inline chat. You can ask Copilot questions that emerge as you write and iterate on code, such as "Explain this piece of code", or "How do I add functionality to do X?".
If you have code selected in the editor, Copilot scopes your question to the selection.
Depending on your question, Copilot can also suggest code modifications. Copilot gives a preview of the updates, which you can then accept (⌘Enter (Windows, Linux Ctrl+Enter)) or discard (Escape).
If you want to ask Copilot a quick question and don't want to start a full Chat view session or open inline chat in your editor, you can use the Quick Chat dropdown. To open Quick Chat, you can run Chat: Open Quick Chat in the Command Palette, or use the ⇧⌘I (Windows Ctrl+Shift+I, Linux Ctrl+Shift+Alt+I) keyboard shortcut.
Chat smart actions
To make it easier to use Copilot Chat features, there is a Copilot menu group in the editor context menu. Right-click in the editor and navigate to Copilot to see the available options:
You can apply these smart actions on the current file or a selection in the file. Choosing an action brings up the Chat view or inline chat, depending on the action. For example, selecting Generate Docs for a function opens the inline chat with a proposed documentation comment:
With the voice control capabilities in VS Code, you have the option to initiate a chat conversation by using your voice.
You can use the "Hey Code" voice command to start a voice session with Copilot Chat. You can choose whether you want this voice command to open the Chat view, inline chat in the editor, quick chat, or choose dynamically based on where the focus is.
In addition, you can accelerate voice input for chat by using the "hold to speak" mode: press and hold the keybinding for inline chat and voice recording automatically starts. As soon as you release the keys, the request is sent to Copilot.
Improving your developer experience
In addition to inline completions and chat, GitHub Copilot can help with other development tasks and workflows. For example, Copilot can help with writing commit messages, fixing errors, and finding commands.
When Copilot can help with a task or workflow, VS Code displays a sparkle icon. Hovering over the sparkle icon describes the Copilot action.
Generate Git commit messages
Copilot can help you write GitHub commit messages. In the Source Control message input box, select the sparkle button at the right and Copilot creates a commit message based on your pending changes.
If you're using the GitHub Pull Request and Issues extension, there is a sparkle button to fill in both the title and description in the Pull Request Create view.
Terminal Quick Fixes
When a command fails to run in the terminal, Copilot displays a sparkle in the gutter that offers a Quick Fix to explain what happened.
Selecting Explain using Copilot populates Quick Chat with the
@terminal #terminalLastCommand agent and variable to help correct the last terminal command error.
Command Palette help
When you're looking for a command in the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)), you can run Ask GitHub Copilot with your search term to help you find the relevant command.
The Ask GitHub Copilot command opens the Chat view and input your search term.
Congratulations, you've now used artificial intelligence to enhance your coding!
You can read more about Copilot and how to use it in VS Code in the GitHub Copilot documentation.