Skip to main content

Getting started with GitHub Copilot in Visual Studio

Learn how to install GitHub Copilot in Visual Studio, and start seeing suggestions as you write comments and code.

GitHub Copilot is available to GitHub customers with a personal account on GitHub.com and GitHub Enterprise Cloud organizations owned by an enterprise account.

GitHub Copilot is free to use for verified students, teachers, and maintainers of popular open source projects. If you are not a student, teacher, or maintainer of a popular open source project, you can try GitHub Copilot for free with a one-time 60-day trial. After the free trial, you will need a paid subscription for continued use. For more information, see "About billing for GitHub Copilot."

尝试 GitHub Copilot

About GitHub Copilot and Visual Studio

GitHub Copilot 在编写代码时提供 AI 对程序员的自动完成样式的建议。 有关详细信息,请参阅“关于 GitHub Copilot”。

If you use Visual Studio, you can view and incorporate suggestions from GitHub Copilot directly within the editor. This guide demonstrates how to use GitHub Copilot within Visual Studio for Windows.

Prerequisites

  • To use GitHub Copilot you must have an active GitHub Copilot subscription. For more information, see "About billing for GitHub Copilot."

  • To use GitHub Copilot in Visual Studio, you must have Visual Studio 2022 17.2 or later installed. For more information, see the Visual Studio IDE documentation.

Note: GitHub Copilot is not currently available for use with Visual Studio for Mac.

Installing the Visual Studio extension

To use GitHub Copilot, you must first install the Visual Studio extension.

  1. In the Visual Studio toolbar, click Extensions, then click Manage Extensions. Screenshot of the Visual Studio toolbar

  2. In the "Manage Extensions" window, click Visual Studio Marketplace, search for the GitHub Copilot extension, then click Download. Screenshot of GitHub Copilot extension for Visual Studio with the download button emphasized

  3. Close the "Manage Extensions" window, then exit and relaunch Visual Studio.

  4. Optionally, to check that GitHub Copilot is installed and enabled, go back to Manage Extensions, click Installed to view your currently installed extensions, then click GitHub Copilot to see status information. Screenshot of installed extensions in Visual Studio with GitHub Copilot emphasized

  5. Open or create a new project in Visual Studio.

  6. In the "Microsoft Visual Studio" dialog box, to copy your device activation code, click OK. Screenshot of the Microsoft Visual Studio dialogue box

  7. A device activation window will open in your browser. Paste the device code, then click Continue.

    • To paste the code in Windows or Linux, press Ctrl+v.
    • To paste the code in macOS, press command+v.
  8. GitHub will request the necessary permissions for GitHub Copilot. To approve these permissions, click Authorize GitHub Copilot Plugin.

  9. After you approve the permissions, Visual Studio will show a confirmation. Screenshot of Visual Studio permissions confirmation

Seeing your first suggestion

Note: If you have duplication detection enabled for GitHub Copilot, you may receive limited suggestions, or no suggestions, when using the code examples provided. As an alternative, you can start by typing your own code to see suggestions from GitHub Copilot. For more information on duplication detection, see "Enabling or disabling duplication detection."

GitHub Copilot 为多种语言和各种框架提供建议,但尤其适用于 Python、JavaScript、TypeScript、Ruby、Go、C# 和 C++。 The following samples are in C#, but other languages will work similarly.
  1. 在 Visual Studio 中,创建新的 C# (*.cs) 文件。
  2. In the C# file, type the following function signature. GitHub Copilot will automatically suggest an entire function body in grayed text, as shown below. The exact suggestion may vary.
    C#
    int CalculateDaysBetweenDates(
    Screenshot of a first suggestion Visual Studio Code
  3. 要接受建议,请按 Tab

Seeing alternative suggestions

对于任何给定的输入,GitHub Copilot 可以提供多个建议。 可以选择要使用的建议,或拒绝所有建议。

  1. 在 Visual Studio 中,创建新的 C# (*.cs) 文件。

  2. In the C# file, type the following function signature. GitHub Copilot will show you a suggestion.

    C#
    int CalculateDaysBetweenDates(
  3. If alternative suggestions are available, you can see these alternatives by pressing Alt+] (or Alt+[).

  4. Optionally, you can hover over the suggestion to see the GitHub Copilot command palette for choosing suggestions.

  5. 若要接受建议,请按 Tab。若要拒绝所有建议,请按 Esc

Generating code suggestions from comments

可以在注释中使用自然语言描述要执行的操作,GitHub Copilot 会提供代码建议来实现你的目标。

  1. 在 Visual Studio 中,创建新的 C# (*.cs) 文件。

  2. In the C# file, type the following comment. GitHub Copilot will suggest an implementation of the function.

    C#
    using System.Xml.Linq;
    
    var doc = XDocument.Load("index.xhml");
    
    // find all images
  3. 要接受建议,请按 Tab

启用或禁用 GitHub Copilot

Visual Studio 窗口底部面板中的 GitHub Copilot 状态图标指示 GitHub Copilot 启用还是禁用。 启用后,图标的背景色将与状态栏颜色相匹配。 禁用后,将有一条对角线穿过它。

  1. 若要启用或禁用 GitHub Copilot,请单击 Visual Studio 窗口底部面板中的 GitHub Copilot 图标。 Visual Studio 中编辑器边距的屏幕截图,其中突出显示 GitHub Copilot 图标

  2. 如果要禁用 GitHub Copilot,系统会询问是全局禁用建议,还是要禁用当前正在编辑的文件的语言。

    • 若要全局禁用 GitHub Copilot 的建议,请单击“全局启用”。
    • 若要禁用指定语言的 GitHub Copilot 的建议,请单击“对 LANGUAGE 启用”。

Further reading