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
-
Install the GitHub Copilot extensions.
-
Sign in with your GitHub account to use Copilot.
TipIf 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.
-
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:
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.
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.
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:
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:
- Open the VS Code Settings editor (⌘, (Windows, Linux Ctrl+,))
- Search for
github.copilot.nextEditSuggestions.enabled
- 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:
If an edit suggestion is below the current editor view, the arrow will point down instead of right:
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
orconts x = 5
, which should've beenconst x = 5
. -
Copilot can also help with more challenging mistakes in logic, like an inverted ternary expression:
Or a comparison that should've used
&&
instead of||
:
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
toPoint3D
, Copilot will suggest to add az
variable to the class definition. After accepting the change, Copilot NES next recommends addingz
to the distance calculation:
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.
-
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.
-
To enable or disable Copilot completions, select Configure Code Completions... from the Copilot menu in the VS Code title bar.
-
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>.
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:
-
Select Configure Code Completions... from the Copilot menu in the VS Code title bar.
-
Select Change Completions Model... from the Configure Copilot Completions menu.
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.