Skip to main content

Getting started with GitHub Copilot in Visual Studio Code

Learn how to install GitHub Copilot in Visual Studio Code, 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 Code

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

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

Prerequisites

Installing the Visual Studio Code extension

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

  1. In the Visual Studio Code Marketplace, go to the GitHub Copilot extension page and click Install. Install GitHub Copilot extension Visual Studio Code
  2. A popup will appear, asking to open Visual Studio Code. Click Open Visual Studio Code.
  3. In the "Extension: GitHub Copilot" tab in Visual Studio Code, click Install. Install button in Visual Studio Code
  4. If you have not previously authorized Visual Studio Code in your GitHub account, you will be prompted to sign in to GitHub in Visual Studio Code.
    • If you have previously authorized Visual Studio Code for your account on GitHub, GitHub Copilot will be automatically authorized. Screen shot of Visual Studio Code authorization screen
  5. In your browser, GitHub will request the necessary permissions for GitHub Copilot. To approve these permissions, click Authorize Visual Studio Code.
  6. In Visual Studio Code, in the "Visual Studio Code" dialog box, to confirm the authentication, click Open.

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 JavaScript, but other languages will work similarly.

  1. 在 Visual Studio Code 中,创建一个新的 JavaScript (*.js) 文件。
  2. In the JavaScript file, type the following function header. GitHub Copilot will automatically suggest an entire function body in grayed text, as shown below. The exact suggestion may vary.
    JavaScript
    function calculateDaysBetweenDates(begin, end) {
    Screenshot of a first suggestion Visual Studio Code
  3. 要接受建议,请按 Tab

Seeing alternative suggestions

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

  1. 在 Visual Studio Code 中,创建一个新的 JavaScript (*.js) 文件。

  2. In the JavaScript file, type the following function header. GitHub Copilot will show you a suggestion.

    JavaScript
    function calculateDaysBetweenDates(begin, end) {
  3. (可选)如果有其他建议,可以选择查看。

    OSSee next suggestionSee previous suggestion
    macOSOption (⌥) or Alt+]Option (⌥) or Alt+[
    WindowsAlt+]Alt+[
    LinuxAlt+]Alt+[
  4. Alternatively, you can hover over the suggestion to see the GitHub Copilot command palette for choosing suggestions.

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

Seeing multiple suggestions in a new tab

你可能不想获得 GitHub Copilot 提供的任何初始建议。 可以使用键盘快捷方式来提示 GitHub Copilot 在新选项卡中显示多个建议。

  1. 在 Visual Studio Code 中,创建一个新的 JavaScript (*.js) 文件。
  2. In the JavaScript file, type the following function header. GitHub Copilot will show you a suggestion.
    JavaScript
    function calculateDaysBetweenDates(begin, end) {
  3. To open a new tab with multiple additional options, press Ctrl+Enter.
  4. To accept a suggestion, above the suggestion, click Accept Solution. To reject all suggestions, close the tab.

Generating code suggestions from comments

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

  1. 在 Visual Studio Code 中,创建一个新的 JavaScript (*.js) 文件。
  2. In the JavaScript file, type the following comment. GitHub Copilot will suggest an implementation of the function.
    JavaScript
    // find all images without alternate text
    // and give them a red border
    function process() {

Using a framework

You can also use GitHub Copilot to generate suggestions for APIs and frameworks. The following example uses GitHub Copilot to create a simple Express server that returns the current time.

  1. 在 Visual Studio Code 中,创建一个新的 JavaScript (*.js) 文件。
  2. In the JavaScript file, type the following comment and then press Enter. GitHub Copilot will suggest an implementation of the Express app.
    JavaScript
    // Express server on port 3000
  3. To accept each line, press Tab, then Enter.
  4. Type the following comment and then press Enter. GitHub Copilot will suggest an implementation for the default handler.
    JavaScript
    // Return the current time
  5. To accept each line, press Tab.

启用或禁用 GitHub Copilot

可以在 Visual Studio Code 中启用或禁用 GitHub Copilot。 Visual Studio Code 窗口底部面板中的 GitHub Copilot 状态图标指示 GitHub Copilot 是启用还是禁用。 启用后,图标的背景色将与状态栏颜色相匹配。 禁用后,图标的背景色将与状态栏颜色形成对比。

  1. 若要启用或禁用 GitHub Copilot,请单击 Visual Studio Code 窗口底部面板中的状态图标。 Visual Studio Code 中状态图标的屏幕截图

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

    • 若要全局禁用 GitHub Copilot 的建议,请单击“全局禁用”。
    • 若要禁用指定语言的 GitHub Copilot 的建议,请单击“对 禁用”。 全局或为当前语言禁用 GitHub Copilot 的选项的屏幕截图

Further reading