Skip to main content

Getting started with GitHub Copilot in a JetBrains IDE

Learn how to install GitHub Copilot in a JetBrains IDE, 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 JetBrains IDEs

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

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

Prerequisites

若要在 JetBrains 中使用 GitHub Copilot,必须安装兼容的 JetBrains IDE。 GitHub Copilot 与以下 IDE 兼容。

  • IntelliJ IDEA(旗舰版、社区版、教育版)
  • Android Studio
  • AppCode
  • CLion
  • Code With Me Guest
  • DataGrip
  • DataSpell
  • GoLand
  • JetBrains Client
  • MPS
  • PhpStorm
  • PyCharm(专业版、社区版、教育版)
  • Rider
  • RubyMine
  • WebStorm

有关详细信息,请参阅 JetBrains IDE 工具查找器。

Installing the GitHub Copilot extension in your JetBrains IDE

To use GitHub Copilot in a JetBrains IDE, you must install the GitHub Copilot extension. The following procedure will guide you through installation of the GitHub Copilot plugin in IntelliJ IDEA. Steps to install the plugin in another supported IDE may differ.

  1. In your JetBrains IDE, under the File menu for Windows or under the name of your IDE for Mac (for example, PyCharm or IntelliJ), click Settings for Windows or Preferences for Mac.

  2. In the left-side menu of the Settings/Preferences dialog box, click Plugins.

  3. At the top of the Settings/Preferences dialog box, click Marketplace. In the search bar, search for GitHub Copilot, then click Install. Screenshot of Marketplace search

  4. After GitHub Copilot is installed, click Restart IDE.

  5. After your JetBrains IDE has restarted, click the Tools menu. Click GitHub Copilot, then click Login to GitHub. Screenshot of JetBrains tools menu

  6. In the "Sign in to GitHub" dialog box, to copy the device code and open the device activation window, click Copy and Open. Screenshot of device code copy and open

  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 the permissions have been approved, your JetBrains IDE will show a confirmation. To begin using GitHub Copilot, click OK. Screenshot of JetBrains IDE 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 Java, but other languages will work similarly.

  1. 在 JetBrains IDE 中,创建新的 Java (*.java) 文件。

  2. In the Java file, create a class by typing class Test. GitHub Copilot will automatically suggest a class body in grayed text, as shown below. The exact suggestion may vary. Screenshot of the Java class body suggestion

  3. 要接受建议,请按 Tab

  4. To prompt GitHub Copilot to suggest a function body, type the following line below the bracket of the main function. The exact suggestion may vary.

    Java
    int calculateDaysBetweenDates(

    Screenshot of the Java function body suggestion

  5. 要接受建议,请按 Tab

GitHub Copilot will attempt to match the context and style of your code. You can always edit the suggested code.

Seeing alternative suggestions

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

  1. 在 JetBrains IDE 中,创建新的 Java (*.java) 文件。

  2. To prompt GitHub Copilot to show you a suggestion, type the following line in the Java file.

    Java
    int calculateDaysBetweenDates(
  3. (可选)如果有其他建议,可以选择查看。

    OSSee next suggestionSee previous suggestion
    macOSOption+]Option+[
    WindowsAlt+]Alt+[
    LinuxAlt+]Alt+[
  4. 若要接受建议,请按 Tab。若要拒绝所有建议,请按 Esc

Seeing multiple suggestions in a new tab

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

  1. 在 JetBrains IDE 中,创建新的 Java (*.java) 文件。
  2. To prompt GitHub Copilot to show you a suggestion, type the following line in the Java file.
    Java
    int calculateDaysBetweenDates(
  3. Open a new tab with multiple additional suggestions.
    • On macOS, press Command+Shift+A, then click Open GitHub Copilot, or press Command+Shift+\ to open the new tab immediately.
    • On Windows or Linux, press Ctrl+Enter, then click Open GitHub Copilot. Screenshot of dialogue to open Copilot
  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. 在 JetBrains IDE 中,创建新的 Java (*.java) 文件。
  2. To prompt GitHub Copilot to suggest an implementation of a function in the Java file, type the following lines.
    Java
    // find all images without alternate text
    // and give them a red border
    void process () {
    Screenshot of the Java function body suggestion

Enabling and disabling GitHub Copilot

You can enable or disable GitHub Copilot for all languages, or for individual languages. The GitHub Copilot status icon in the bottom panel of your JetBrains IDE window indicates whether GitHub Copilot is enabled or disabled. When enabled, the icon is highlighted. When disabled, the icon is grayed out.

  1. To enable or disable GitHub Copilot, click the status icon in the bottom panel of the JetBrains window. Screenshot of the status icon in IntelliJ IDEA

  2. If you are disabling GitHub Copilot, you will be asked whether you want to disable it globally, or for the language of the file you are currently editing.

    • To disable suggestions from GitHub Copilot globally, click Disable Completions.
    • To disable suggestions from GitHub Copilot for the specified language, click Disable Completions for LANGUAGE. Screenshot of option to disable GitHub Copilot globally or for the current language

Further reading