Redigera

Dela via


What is GitHub Copilot Completions for Visual Studio?

GitHub Copilot in Visual Studio enables enhanced AI-assisted development in Visual Studio, helping you be more productive and efficient when writing code.

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.

For more information on GitHub Copilot Chat in Visual Studio, see About GitHub Copilot Chat in Visual Studio.

How GitHub Copilot works

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.

Animated screenshot that shows the code completion capabilities of the GitHub Copilot extension.

You can also use GitHub Copilot to convert comments to code, create unit tests, create SQL queries, and more.

Prerequisites

To get started, you need:

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.
  • If you're using Visual Studio versions 17.9 or earlier, install the GitHub Copilot extension.

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.

  1. Open Visual Studio and create a new C# project.

  2. Open a C# file (Program.cs) in the editor.

  3. In the code editor, type a comment or a method signature to see inline code suggestions from GitHub Copilot.

        // method to add two numbers
    
        int subtractNumbers(
    
  4. Hover over the suggestion to see the GitHub Copilot command palette.

    Accept suggestion

    Select Tab or press Tab to accept the suggestion or continue typing to reject it.

    Screenshot that shows accepting the GitHub Copilot code suggestions in Visual Studio

    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.

    Screenshot that shows modifying the GitHub Copilot code suggestions in Visual Studio

    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.

  5. Add following code to see whole line completions from GitHub Copilot.

        int a = 5;
        int b = 10;
        int sum
    

    Animated screenshot that shows using the GitHub Copilot completions in Visual Studio.

Note

With Visual Studio 2022 version 17.11, content exclusion is available for GitHub Copilot Completions in Visual Studio. Completions and suggestions won't be available on content excluded by your admin. See configuring content exclusions for GitHub Copilot to learn more.

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: