Skip to main content

Getting code suggestions in your IDE with GitHub Copilot

Use GitHub Copilot to get code suggestions in your editor.

About GitHub Copilot and Visual Studio

This guide demonstrates how to get coding suggestions from GitHub Copilot in Visual Studio for Windows. To see instructions for other popular coding environments, use the tool switcher at the top of the page.

The examples in this guide use C#, however other languages will work similarly. GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, but works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. GitHub Copilot can also assist in query generation for databases, generating suggestions for APIs and frameworks, and can help with infrastructure as code development.

Prerequisites

  • Access to Copilot. To use GitHub Copilot in GitHub Copilot in Visual Studio, you need either limited access through Copilot Free or a paid Copilot plan for full access. For information about how to get access to Copilot, see What is GitHub Copilot?.

  • Compatible version of Visual Studio. To use GitHub Copilot in Visual Studio, you must have version 2022 17.8 or later of Visual Studio for Windows installed. For more information, see Install Visual Studio in the Microsoft documentation.

  • GitHub Copilot extension for Visual Studio. For instructions on how to install the Copilot extension, see Install GitHub Copilot in Visual Studio in the Microsoft documentation.

  • Add your GitHub account to Visual Studio. See Add your GitHub accounts to your Visual Studio keychain in the Microsoft documentation.

Getting code suggestions

GitHub Copilot offers coding suggestions as you type. For example, type this function signature in a C# file:

C#
int CalculateDaysBetweenDates(

GitHub Copilot will automatically suggest an entire function body in grayed text. To accept the suggestion, press Tab.

You can also describe something you want to do using natural language within a comment, and Copilot will suggest the code to accomplish your goal. For example, type this comment in the C# file:

C#
using System.Xml.Linq;

var doc = XDocument.Load("index.xhml");

// find all images

GitHub Copilot will suggest an implementation of the function. To accept the suggestion, press Tab.

Tip

If you receive limited or no suggestions from Copilot, you may have duplication detection enabled. For more information about duplication detection, see Managing Copilot policies as an individual subscriber.

Showing alternative suggestions

For any given input, GitHub Copilot may offer multiple suggestions. You can select which suggestion to use, or reject all suggestions.

For example, type this function signature in a C# file:

C#
int CalculateDaysBetweenDates(

GitHub Copilot will show you a suggestion.

Now hover over the suggestion to show the GitHub Copilot control for choosing suggestions. To display next or previous suggestions, click the forward or back arrow button in the control.

Alternatively, you can show alternate suggestions by pressing Alt+. (or Alt+,) on your keyboard.

To accept a suggestion, click "Accept" in the Copilot command palette, or press Tab. To reject all suggestions, press Esc.

Getting comment suggestions

Note

  • Comment suggestions are currently in public preview and are subject to change.
  • Comment suggestions are available in Visual Studio 17.14 Preview 2 and later.

GitHub Copilot can suggest comments for your code, by analyzing the code you write and generating comments that describe what the code does. For Copilot Free users, comment suggestions count towards your monthly Copilot Chat usage, not your code suggestions usage.

Comment suggestions are available in the following languages:

  • C#
  • C++

Enabling comment suggestions

To enable comment suggestions, you need to configure the comment style in Visual Studio.

For C++

  1. In Visual Studio, in the Tools menu, click Options.
  2. In the left-side panel, click Text Editor.
  3. Click C++, then Code Style, then General.
  4. Under "Comments", select Xml Doc Comments from the dropdown.
  5. Select Insert existing comment style at the start of new lines when writing comments and Continue single line comments.

For C#

  1. In Visual Studio, in the Tools menu, click Options.
  2. In the left-side panel, click Text Editor.
  3. Click C#, then Advanced.
  4. Under "Comments", select Generate XML documentation comments for ///, Insert // at the start of new lines when writing // comments, and Insert * at the start of new lines when writing /* */ comments.

Using comment suggestions

To initiate comment suggestions, type the standard comment initiator for the language you are writing in (for example, ///), before the code you want to comment, and wait for the suggestion to appear.

To accept the suggestion, press Tab. To modify the suggestion, press Alt+/. To reject the suggestion, press Esc.

About next edit suggestions

Inline suggestions autocomplete code, but many development tasks involve editing existing code. Next edit suggestions assists with edits both at the cursor and in other relevant parts of the code, helping maintain consistency and streamline changes.

Next edit suggestions predicts where and what edits may be needed based on ongoing changes. Suggestions may span a single symbol, an entire line, or multiple lines, depending on the scope of the potential change.

To enable next edit suggestions, see Configuring GitHub Copilot in your environment.

You can navigate suggested code changes using Tab, making it easier to find the next relevant edit without manually searching through files or references. Press Tab again to accept a suggestion.

An arrow in the gutter indicates an available edit suggestion. Click the arrow to access the edit suggestion menu, which provides keyboard shortcuts. If an edit suggestion is outside the current editor view, the arrow will point up or down to indicate where the next suggestion is.

Next steps