🚀 Get in VS Code!

Code completions with GitHub Copilot in VS Code

GitHub Copilot acts as an AI-powered pair programmer, automatically offering suggestions to complete your code, comments, tests, and more. It provides these suggestions directly in the editor while you write your code, and it can work with a broad range of programming languages and frameworks.

Copilot provides two kinds of suggestions:

  • Code completions - Start typing in the editor, and Copilot provides code suggestions that match your coding style and take your existing code into account.

  • Next Edit Suggestions (preview) - Predict your next code edit with Copilot Next Edit Suggestions, aka Copilot NES. Based on the edits you're making, Copilot NES both predicts the location of the next edit you'll want to make and what that edit should be.

Getting started

  1. Install the GitHub Copilot extensions.

    Install the GitHub Copilot extensions

  2. Sign in with your GitHub account to use Copilot.

    Tip

    If you don't yet have a Copilot subscription, you can use Copilot for free by signing up for the Copilot Free plan and get a monthly limit of completions and chat interactions.

  3. Discover the key features of Copilot in VS Code with our Copilot Quickstart.

Inline suggestions

Copilot offers code suggestions as you type: sometimes the completion of the current line, sometimes a whole new block of code. You can accept all, or part of a suggestion, or you can keep typing and ignore the suggestions.

Notice in the following example how Copilot suggests an implementation of the calculateDaysBetweenDates JavaScript function by using dimmed ghost text:

JavaScript ghost text suggestion.

When you're presented with an inline suggestion, you can accept it with the Tab key.

Copilot tries to apply the same coding style for the code suggestions that you already have in your code. Notice in the following example that Copilot applies the same input parameter naming scheme from the add method for the suggested subtract method.

JavaScript ghost text suggestion.

Partially accepting suggestions

You might not want to accept an entire suggestion from GitHub Copilot. You can use the ⌘→ (Windows, Linux Ctrl+Right) keyboard shortcut to accept either the next word of a suggestion, or the next line.

Alternative suggestions

For any given input, Copilot might offer multiple, alternative suggestions. You can hover over the suggestion to any of the other suggestions.

Hovering over inline suggestions enables you to select from multiple suggestions

Generate suggestions from code comments

Instead of relying on Copilot to provide suggestions, you can provide hints about what code you expect by using code comments. For example, you could specify a type of algorithm or concept to use (for example, "use recursion" or "use a singleton pattern"), or which methods and properties to add to a class.

The following example shows how to instruct Copilot to create a class in TypeScript to represent a student, providing information about methods and properties:

Use code comments to let Copilot generate a Student class in TypeScript with properties and methods.

Next Edit Suggestions (preview)

Inline suggestions are great at autocompleting a section of code. But since most coding activity is editing existing code, it's a natural evolution of Copilot code completions to also help with edits, both at the cursor and further away. Edits are often not made in isolation - there's a logical flow of what edits need to be made in different scenarios. Copilot Next Edit Suggestions (Copilot NES) is this evolution.

Based on the edits you're making, Copilot NES both predicts the location of the next edit you'll want to make and what that edit should be. Copilot NES helps you stay in the flow, suggesting future changes relevant to your current work, and you can simply Tab to quickly navigate and accept Copilot's suggestions. Suggestions may span a single symbol, an entire line, or multiple lines, depending on the scope of the potential change.

Enabling edit suggestions

Copilot NES is currently in preview. You can enable it via the VS Code setting github.copilot.nextEditSuggestions.enabled:

  1. Open the VS Code Settings editor (⌘, (Windows, Linux Ctrl+,))
  2. Search for github.copilot.nextEditSuggestions.enabled
  3. Enable the setting

If you are a Copilot Business or Enterprise user, an administrator of your organization must opt in to the use of Copilot "Editor Preview Features," in addition to you setting github.copilot.nextEditSuggestions.enabled in your editor.

You can learn more about managing policies for Copilot in your organization.

Navigate and accept edit suggestions

You can quickly navigate suggested code changes with the Tab key, saving you time to find the next relevant edit (no manual searching through files or references required). You can then accept a suggestion with the Tab key again.

An arrow in the gutter indicates if there is an edit suggestion available. You can hover over the arrow to explore the edit suggestion menu, which includes keyboard shortcuts and settings configuration: Copilot NES gutter menu expanded

If an edit suggestion is below the current editor view, the arrow will point down instead of right: Copilot NES with arrow directions changing

Use cases for Next Edit Suggestions

Catching and correcting mistakes

  • Copilot helps with simple mistakes like typos. It'll suggest fixes where letters are missing or swapped, like cont x = 5 or conts x = 5, which should've been const x = 5.

    Copilot NES fixing a typo from "conts" to "const"

  • Copilot can also help with more challenging mistakes in logic, like an inverted ternary expression:

    Copilot NES fixing a fibonacci logic mistake

    Or a comparison that should've used && instead of ||:

    Copilot NES fixing an if statement mistake

Changing intent

  • Copilot suggests changes to the rest of your code that match a new change in intent. For example, when changing a class from Point to Point3D, Copilot will suggest to add a z variable to the class definition. After accepting the change, Copilot NES next recommends adding z to the distance calculation:

    Copilot NES gif for updating Point to Point3D

Refactoring

  • Rename a variable once in a file, and Copilot will suggest to update it everywhere else. If you use a new name or naming pattern, Copilot suggests to update subsequent code similarly.

    Copilot NES suggesting change after updating function name

  • Matching code style. After copy-pasting some code, Copilot will suggest how to adjust it to match the current code where the paste happened.

Further configuring edit suggestions

To use Copilot NES, you must set github.copilot.nextEditSuggestions.enabled. There are a couple of additional settings you can also use to configure your Copilot NES experience:

  • editor.inlineSuggest.edits.codeShifting: You can disable this setting if you never want Copilot NES to shift your code to show a suggestion.

  • editor.inlineSuggest.edits.renderSideBySide:

    • auto (default): Show larger suggestions side-by-side if there is enough space in the viewport, otherwise the suggestions are shown below the relevant code.
    • never: Always show larger suggestions below the relevant code.

Tips & tricks

Context

To give you relevant inline suggestions, Copilot looks at the current and open files in your editor to analyze the context and create appropriate suggestions. Having related files open in VS Code while using Copilot helps set this context and lets Copilot get a bigger picture of your project.

Enable or disable code completions

You can temporarily enable or disable code completions either for all languages, or for specific languages only.

  1. To enable or disable Copilot completions, select Configure Code Completions... from the Copilot menu in the VS Code title bar.

    Screenshot showing Copilot menu, highlighting Configure Code Completions.

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

    • To disable suggestions from GitHub Copilot globally, select Disable Globally.
    • To disable suggestions from GitHub Copilot for the specified language, select Disable for <language>.

    Screenshot showing the VS Code command menu for Copilot, highlighting the options to disable completions.

Change the AI model

Different Large Language Models (LLMs) are trained on different types of data and might have different capabilities and strengths. With Copilot, you can change the language model that is used to generate completions. To change the current model:

  1. Select Configure Code Completions... from the Copilot menu in the VS Code title bar.

    Screenshot showing Copilot menu, highlighting Configure Code Completions.

  2. Select Change Completions Model... from the Configure Copilot Completions menu.

    Screenshot showing Copilot Completions menu, highlighting option to change completions model

Note

The list of available models might vary and change over time. If you are a Copilot Business or Enterprise user, you will also need your Administrator to enable certain models for your organization by opting in to Editor Preview Features in the Copilot policy settings on GitHub.com.

Settings

  • editor.inlineSuggest.enabled - enable or disable inline completions.

  • editor.inlineSuggest.fontFamily - configure the font for the inline completions.

  • editor.inlineSuggest.showToolbar - enable or disable the toolbar that appears for inline completions.

  • editor.inlineSuggest.syntaxHighlightingEnabled - enable or disable syntax highlighting for inline completions.

  • github.copilot.nextEditSuggestions.enabled - enable Copilot Next Edit Suggestions (Copilot NES).

  • editor.inlineSuggest.edits.codeShifting - configure if Copilot NES is able to shift your code to show a suggestion.

  • editor.inlineSuggest.edits.renderSideBySide - configure if Copilot NES can show larger suggestions side-by-side if possible, or if Copilot NES should always show larger suggestions below the relevant code.

Next steps

  • Get started with the introductory Copilot tutorial to get set up with Copilot in VS Code and experience Copilot hands-on.

  • Learn how you can use AI chat conversations with Copilot Chat.

Additional resources

You can read more about Copilot and how to use it in VS Code in the GitHub Copilot documentation.

Or check out the VS Code Copilot Series on YouTube, where you can find more introductory content and programming-specific videos for using Copilot with Python, C#, Java, PowerShell, C++, and more.