Skip to main content

Getting code suggestions in your IDE with GitHub Copilot

Use GitHub Copilot to get code suggestions in your editor.

Introduction

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.

For more information, see GitHub Copilot code suggestions in your IDE.

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. 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 GitHub 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.

Based on the edits you are making, Copilot will predict the location of the next edit you are likely to make and suggest a completion for it.

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

Further reading