In this article, you learn about Copilot Completions, an AI-powered pair programmer for Visual Studio that provides you with context-aware code completions, suggestions, and even entire code snippets. This powerful tool aids AI-assisted development in Visual Studio and helps developers write code more efficiently, reduce the time spent on repetitive tasks, and minimize errors.
GitHub Copilot works by utilizing advanced machine learning models trained on a vast dataset of publicly available code from GitHub repositories. As you type code, the AI analyzes the context and provides relevant suggestions in real-time. You can receive suggestions also by writing a natural language comment describing what you want the code to do.
You can also use GitHub Copilot to convert comments to code, create unit tests, create SQL queries, and more.
GitHub Copilot supports several programming languages and frameworks, including but not limited to: C#, C++, and Python.
Get GitHub Copilot for Visual Studio
If you're using Visual Studio version 17.10 or later, the unified GitHub Copilot extension is available as a recommended component in the Visual Studio Installer. It is installed by default with all workloads, unless you choose to exclude it during installation.
Using GitHub Copilot for code completions and suggestions
As you type code or comments in the editor, GitHub Copilot provides context-aware code completions and suggestions. GitHub Copilot provides suggestions for numerous languages, but works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. The following examples are in C#, but other languages will work similarly.
Open Visual Studio and create a new C# project.
Open a C# file (Program.cs) in the editor.
In the code editor, type a comment or a method signature to see inline code suggestions from GitHub Copilot.
C#
// method to add two numbers
C#
intsubtractNumbers(
Hover over the suggestion to see the GitHub Copilot command palette.
Accept suggestion
Select Tab or press Tabto accept the suggestion or continue typing to reject it.
Modify suggestion
Select Alt+/ or press Alt+/to modify the suggestion from Copilot. You can modify and curate the GitHub Copilot suggestions by adding context in inline chat or by editing the completion.
If the command palette doesn't appear, you might have reached the default limit for its display. To change this, go to Tools > Options > IntelliCode > Advanced, and adjust the value for Minimum commits to suppress hint text.
Add following code to see whole line completions from GitHub Copilot.
With Visual Studio 2022 version 17.13, code referencing is available for GitHub Copilot Completions in Visual Studio. If you or your organization have enabled suggestions that match public code, Copilot will notify you when you accept a completion that matches code from a public GitHub repository.
:::
Select View code matches to see detailed information in GitHub Copilot logs in the Output window. The log entry includes a link to a GitHub.com page where you can view details on license type and references to similar code in public GitHub repositories.
Code referencing enables you to make an informed decision on using code attribution or removing the code from your project.
Exploring GitHub Copilot
To see GitHub Copilot in action, check out the following video tutorial.
Video length: 1.19 minutes
Next steps
To learn more about the current GitHub Copilot extension for Visual Studio, consider exploring the following resources:
This module explores using GitHub Copilot autocompletion suggestions to create new code. Autocompletion suggestions are generated, managed, and implemented using the GitHub Copilot extension for Visual Studio Code.
Learn about the fully integrated GitHub Copilot Chat in Visual Studio. Use the chat interface to ask coding-related questions from right within the IDE.
Learn how to install GitHub Copilot to get GitHub Copilot Completions and Chat in Visual Studio. Use the Copilot badge in the IDE to access options to manage or troubleshoot your Copilot state.
Add your public GitHub or GitHub Enterprise account to your Visual Studio keychain so you can track code changes in GitHub repositories from Visual Studio.