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 errors 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.
Prerequisites
-
To use GitHub Copilot in VS Code, you must have the GitHub Copilot extension. When you install this extension, the GitHub Copilot Chat extension is also installed.
-
To use GitHub Copilot, you must have an active subscription for GitHub Copilot in your personal account, or you need to be assigned a seat by your organization. Sign up for a GitHub Copilot free trial in your personal account.
Follow these steps to Set up GitHub Copilot in VS Code.
Copilot tutorials
- GitHub Copilot Quickstart - discover the key features of GitHub Copilot in VS Code.
- Copilot Chat Tutorial - get started with AI chat conversations.
Use cases for GitHub Copilot in VS Code
GitHub Copilot can provide coding assistance in a variety of scenarios:
- Code completions in the editor
- Answering coding questions
- Code refactoring and improvements
- Fix issues
- Jumpstart your project
- Generate unit test cases
- Generate code documentation
- Improve your productivity in VS Code
Code completions in the editor
-
Copilot suggests code as you type. Copilot analyzes the context in the file you're editing and related files, and offers suggestions from within the editor. For example, begin typing the name of a method and Copilot suggests the implementation, following your coding style.
Answering coding questions
-
Provide guidance and support for common coding tasks and challenges. Ask Copilot about syntax or general programming concepts without requiring to navigate documentation or search online forums. Copilot gives responses in natural language format or in code snippet format. For example, you can ask questions such as "what is recursion?" or "how to create a singleton in Java?".
-
Improve code understanding by explaining selected code. Copilot generates natural language descriptions of the code's functionality and purpose. This can be useful if you want to understand the code's behavior or for non-technical stakeholders who need to understand how the code works.
-
Provide guidance that is specific to your codebase. Copilot has the context of your workspace and can give step-by-step guidance and code samples that are tailored to your project. For example, "how to add a contacts page?" or "how do I read customer data from the database?".
Code refactoring and improvements
-
Provide suggestions for implementing code refactorings. Copilot suggests refactorings using the context of your codebase. For example, ask Copilot to refactor a function to not use recursion, or to suggest an algorithm that can improve performance.
-
Suggest potential improvements to selected code, such as improved handling of errors and edge cases, or changes to the logical flow to make the code more readable.
Fix issues
-
Propose a fix for bugs in your code with the
/fix
command by suggesting code snippets and solutions based on the context of the error or issue. For example, if your code produces an error message or warning, Copilot Chat can suggest possible fixes based on the error message, the code's syntax, and the surrounding code. The changes might consist of changes to variables, control structures, or function calls that could resolve the issue. -
Suggest terminal command 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.
Jumpstart your project
-
Generate a new VS Code workspace for your choice of technologies with
/new
to get started quickly with a new project. Pick and choose your technology stack, preview the workspace files, and let Copilot scaffold the entire workspace for you. -
Scaffold a new Jupyter notebook by using natural language. Generate a new notebook that is preconfigured based on a description by using
/newNotebook
. For example, to scaffold a new notebook that loads, inspects, and visualizes a sample dataset, prompt Copilot with "@workspace /newNotebook read titanic dataset with pandas, display key values with seaborn".
Generate unit test cases
-
Write unit test cases for your test framework based on the code open in the editor or the code snippet you highlight in the editor. Copilot identifies your test framework and coding style and generates matching code snippets.
-
Identify and write test cases for edge cases and boundary conditions that might be difficult to identify manually. For instance, Copilot can suggest test cases for error handling, null values, or unexpected input types.
-
Suggest assertions that ensure the function is working correctly, based on the code's context and semantics. For example, generate assertions to ensure that function input parameters are valid.
Generate code documentation
-
Generate code documentation for multiple languages for the code open in the editor or the code snippet you highlight in the editor. Use
/doc
or a Copilot smart action to help you generate meaningful code documentation.
Productivity improvements
-
AI-generated commit messages and PR descriptions based on the code changes in a commit or the changes in a pull request. Use the sparkle button in the Source Control view or GitHub PR extension to generate a title and description that summarizes your changes.
-
Ask help in the Command Palette to help you find the relevant command in VS Code. You can describe the functionality and Copilot can help identify the matching functionality. For example, type "code preview in scrollbar" in the Command Palette, and Copilot can identify that you're referring to the
editor.minimap
settings. -
Use terminal inline chat to ask questions about the terminal or how to use specific shell commands. For example, you can ask questions such as "list the top 5 largest files in the src directory", or "how to enable shell integration".
-
AI-generated rename suggestions for symbols in your source code. When you rename a symbol in your code, Copilot suggests a new name based on the context of the symbol and the codebase.
Additional resources
You can read more about Copilot and how to use it in VS Code in the GitHub Copilot documentation.
Or check out the VS Code Copilot Series on YouTube, where you can find more introductory content and programming-specific videos for using Copilot with Python, C#, Java, PowerShell, and more.