How to use Comments as Prompts in GitHub Copilot for Visual Studio
Published Mar 21 2024 12:01 AM 3,680 Views
Microsoft

GitHub Copilot is a coding assistant powered by Artificial Intelligence (AI), which can run in various environments and help you be more efficient in your daily coding tasks. In this new series of content, we will show you how GitHub Copilot works in Visual Studio specifically and how it helps you being more productive.

 

In the second short video in this series, my colleague Bruno Capuano will use comments to trigger GitHub Copilot to generate code directly inline in the current file.


  
In case you are not sure, check how to install GitHub Copilot for Visual Studio in the first article of this series.

 

What’s interesting with GitHub Copilot for Visual Studio is that it’s not just suggesting code, but it is also suggesting completion for your comments. This can be quite useful when you write documentation, for example.

 

In the example, Bruno is typing a comment looking like this:

 

 

// function to get the year of birth

 

 

Immediately Copilot jumps to action and proposes to complete the comment as follows:

 

 

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

 

 

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

 

In order to validate the suggestion and add it to the code, you can simply press the Tab key. Then, if you press Enter, Copilot is going to suggest some code. Based on your internet connection, this can be either immediate or take a couple of seconds. The code suggestion will also be added inline and greyed out. And here too, you can accept the suggestion by pressing the Tab key.
 

Validating the output

The most important concept to keep in mind when dealing with Artificial Intelligence is that the user (that’s you) should always validate the output of the model. You have the opportunity to check the Copilot output when the code is added and greyed out, or after you press the Tab key. I often accept the suggestion and then go back to the generated code to change small details to “make the code mine” or even to fix it.

 

GitHub Copilot is not a compiler!

 

What this means is that the code generated by GitHub Copilot can very well fail compilation, leaving you, the Pilot in charge, responsible to check and correct the code.

Disabling completion

Note that you might not always want Copilot to suggest completions in your code files. It’s quite easy to deactivate it, either 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.

 

LBugnion_0-1710942845668.png

LBugnion_1-1710942916712.png
 

More information

For more information, check our collection with resources here. Also, you can see the full length video here. Stay tuned to this blog for more content posted regularly. And of course, you can also subscribe to our YouTube channel to get more video content!

Co-Authors
Version history
Last update:
‎Mar 26 2024 01:50 AM
Updated by: