0% found this document useful (0 votes)
9 views12 pages

04-Code completions with GitHub Copilot in VS Code

GitHub Copilot in VS Code acts as an AI-powered assistant that offers code completions and next edit suggestions while coding. Users can install the Copilot extension, sign in with their GitHub account, and utilize features like inline suggestions, alternative suggestions, and the ability to generate suggestions from comments. The tool helps streamline coding by predicting edits and maintaining coding style consistency across various programming languages.

Uploaded by

nicobar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

04-Code completions with GitHub Copilot in VS Code

GitHub Copilot in VS Code acts as an AI-powered assistant that offers code completions and next edit suggestions while coding. Users can install the Copilot extension, sign in with their GitHub account, and utilize features like inline suggestions, alternative suggestions, and the ability to generate suggestions from comments. The tool helps streamline coding by predicting edits and maintaining coding style consistency across various programming languages.

Uploaded by

nicobar14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Code completions with GitHub Copilot in VS Code https://code.visualstudio.

com/docs/copilot/ai-powered-suggestions

🚀 Get GitHub Copilot Free (vscode://github.copilot-chat?referrer=vscode-updatebanner) in VS


Code!
×

TOPICS Code Completions

IN THIS ARTICLE Getting started

https://vscode.dev/github/microsoft/vscode-docs/blob/main/docs/copilot/ai-powered-suggestions.md)

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 (vscode:extension/GitHub.copilot?


referrer=docs-copilot-ai-powered-suggestions)

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 (https://github.com/github-copilot/signup) and get a monthly limit of
completions and chat interactions.

1 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

3 Discover the key features of Copilot in VS Code with our Copilot Quickstart (/docs/copilot/getting-
started).

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

2 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

You might not want to accept an entire suggestion from GitHub Copilot. You can use the ⌘→ 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:

3 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

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
! (vscode://settings/
github.copilot.nextEditSuggestions.enabled)github.copilot.nextEditSuggestions.enabled
:

1 Open the VS Code Settings editor ( ⌘, )

2 Search for github.copilot.nextEditSuggestions.enabled

4 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

3 Enable the setting

Important
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
! (vscode://settings/
github.copilot.nextEditSuggestions.enabled)github.copilot.nextEditSuggestions.enabled
in your editor. Learn more about managing policies for Copilot in your organization (https://
docs.github.com/en/copilot/managing-copilot/managing-github-copilot-in-your-organization/
managing-policies-for-copilot-in-your-organization#enabling-copilot-features-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:

5 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

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 can also help with more challenging mistakes in logic, like an inverted ternary expression:

6 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

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 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:

7 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

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.

Edit suggestions configuration options


To further configure edit suggestions, configure these settings:

! (vscode://settings/
• editor.inlineSuggest.edits.codeShifting)editor.inlineSuggest.edits.codeShifting
: disable this setting if you never want Copilot NES to shift your code when showing a suggestion.

! (vscode://settings/
• editor.inlineSuggest.edits.renderSideBySide)editor.inlineSuggest.edits.renderSideBySide
:

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

8 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

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 completions:

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

2 Select Enable Completions.

To disable completions:

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

2 To disable completions globally, select Disable Globally.

3 To disable completions for the language of the file you are currently editing, 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. You can change the language model that is used to generate code completions.

To change the model that is used for code completions:

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

2 Select Change Completions Model..., and then select one of the models.

Note
The list of available models might vary and change over time. If you are a Copilot Business or
Enterprise user, your Administrator needs to enable certain models for your organization by opting in
to Editor Preview Features in the Copilot policy settings (https://docs.github.com/en/enterprise-
cloud@latest/copilot/managing-copilot/managing-github-copilot-in-your-organization/managing-

9 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

policies-for-copilot-in-your-organization#enabling-copilot-features-in-your-organization) on
GitHub.com.

Settings

Code completions settings


• ! (vscode://settings/editor.inlineSuggest.enabled)editor.inlineSuggest.enabled - enable or
disable inline completions.

• ! (vscode://settings/editor.inlineSuggest.fontFamily)editor.inlineSuggest.fontFamily - configure
the font for the inline completions.

• ! (vscode://settings/editor.inlineSuggest.showToolbar)editor.inlineSuggest.showToolbar - enable
or disable the toolbar that appears for inline completions.

! (vscode://settings/
• editor.inlineSuggest.syntaxHighlightingEnabled)editor.inlineSuggest.syntaxHighlightingEnabled
- enable or disable syntax highlighting for inline completions.

Next Edit Suggestions settings

! (vscode://settings/
• github.copilot.nextEditSuggestions.enabled)github.copilot.nextEditSuggestions.enabled
- enable Copilot Next Edit Suggestions (Copilot NES).

! (vscode://settings/
• editor.inlineSuggest.edits.codeShifting)editor.inlineSuggest.edits.codeShifting
- configure if Copilot NES is able to shift your code to show a suggestion.

! (vscode://settings/
• editor.inlineSuggest.edits.renderSideBySide)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

• Discover the key features with the Copilot Quickstart (/docs/copilot/getting-started-chat).

• Use AI chat conversations with Copilot Chat (/docs/copilot/copilot-chat).

• Watch the videos in our VS Code Copilot Series (https://www.youtube.com/playlist?


list=PLj6YeMhvp2S5_hvBl2SE-7YCHYlLQ0bPt) on YouTube.

10 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

Was this documentation helpful?

Yes No

02/06/2025

Subscribe(/feed.xml) Ask questions(https://stackoverflow.com/questions/tagged/vscode)

Follow @code(https://go.microsoft.com/fwlink/?LinkID=533687)

Request features(https://go.microsoft.com/fwlink/?LinkID=533482)

Report issues(https://www.github.com/Microsoft/vscode/issues)

Watch videos(https://www.youtube.com/channel/UCs5Y5_7XK8HLDX0SLNwkd3w)

11 sur 12 19/02/2025 17:30


Code completions with GitHub Copilot in VS Code https://code.visualstudio.com/docs/copilot/ai-powered-suggestions

(https://
www.microsoft.com)

(https://go.microsoft.com/fwlink/?LinkID=533687)
(https://github.com/microsoft/vscode) (https://www.youtube.com/@code)

Support (https://support.serviceshub.microsoft.com/supportforbusiness/create?sapId=d66407ed-3967-
b000-4cfb-2c318cad363d)
Privacy (https://go.microsoft.com/fwlink/?LinkId=521839)
Terms of Use (https://www.microsoft.com/legal/terms-of-use) License (/License)

12 sur 12 19/02/2025 17:30

You might also like