How to use Comments to Prompt GitHub Copilot for Visual Studio

Gwyneth Peña-Siguenza

Laurent Bugnion

Image Comments as Prompts in GitHub Copilot for Visual Studio

A step-by-step guide to use comments as prompts in GitHub Copilot for Visual Studio

Introduction to GitHub Copilot for Visual Studio

GitHub Copilot is a programming assistant that uses AI (Artificial Intelligence) to help you increase efficiency in your daily programming tasks. It is compatible with various IDEs (Integrated Development Environments), text editors, and more. In this series, we will focus on demonstrating how GitHub Copilot can help you become more productive in Visual Studio.

One way to get code suggestions from GitHub Copilot is by leveraging code comments. However, we know sometimes even coming up with a comment can be a little cumbersome. 

What is great about GitHub Copilot for Visual Studio is that it is not only capable of suggesting code but can also provide completions for your comments.

In the second short video in this series, my colleague Bruno Capuano will highlight how to use comments to prompt GitHub Copilot to produce code directly within the current file.

Make sure to have GitHub Copilot installed, refer to the documentation to learn how to install GitHub Copilot for Visual Studio

How to use a Comment to Prompt GitHub Copilot

Let us start by writing a comment. In the example, Bruno is typing a comment that looks like this:

// function to get the year of birth

As we can see in this example, GitHub Copilot quickly springs into action, proposing to complete the comment as follows:

 // function to get the year of birth *from the age*

This feature can be particularly useful, for example, when you are writing documentation.

(Note: in this example I added *stars* around the suggested code. In the Visual Studio IDE, the suggestion would appear greyed out).

To accept the comment completion suggestion and incorporate it into your code, simply press the Tab key. Following that, pressing Enter prompts Copilot to offer a code suggestion based off the comment. The suggested code will appear inline and greyed out. You can also accept this suggestion by pressing the Tab key.

Additional examples can be found here in Using GitHub Copilot for code completions and suggestions


Validating the output of GitHub Copilot

The key principle to remember when working with AI assistants is that you should always verify the output. You have the chance to review the Copilot output either before accepting, when the code appears greyed out inline or after you have accepted the suggestion with the Tab key. I frequently accept the suggestion first and then revisit the generated code. If I need to make any changes, I will do so before moving on.

GitHub Copilot is not a compiler!

This means that the code produced by GitHub Copilot can indeed fail to compile and/or be incorrect. It is your responsibility to review, resolve, and improve the suggested code, as necessary.

Disabling GitHub Copilot completions

You might not always want Copilot to suggest completions in your code files. Deactivating it is straightforward, whether for all code files, or even just for specific files such as markdown or C#. Simply click on the small Copilot icon on the Visual Studio taskbar and then select the appropriate option.

Image Picture1

Image Picture2


Now that you have successfully used a comment to GitHub Copilot in Visual Studio, you can continue to leverage this practice in all your projects. This is just one of the ways that GitHub Copilot can help you write code faster and be more productive. To learn more about GitHub Copilot and how to use it, check our collection with resources here or via our full-length video.

Additional Resources

Feedback usabilla icon