Getting Started With GitHub Copilot in Visual Studio Code

GitHub Copilot is an AI pair programming tool. This is a fancy way of calling it a “second programmer” that works within your source code editor. Copilot gives you autocomplete-style suggestions as you code, helping you code faster and more efficiently.

This article will walk you through the steps for installing and setting up GitHub Copilot in VS Code. You’ll then learn how to use Copilot to speed up the coding process. 


To use GitHub Copilot, you need to have a GitHub account. If you don’t already have one, you can register for an account on the official website

Make sure you’re signed into your GitHub account before signing up for GitHub Copilot. The service comes with a 30-day free trial, after which you’re required to subscribe for one of the paid plans.

Screenshot of GitHub copilot authorization pageScreenshot of GitHub copilot authorization pageScreenshot of GitHub copilot authorization page
Screenshot of GitHub copilot authorization page

If you have no intention of subscribing for a paid plan, then make sure you cancel GitHub Copilot before the trial ends to avoid getting billed.

Finally, you’ll need to have Visual Studio Code installed on your local machine. To install Visual Studio Code, visit the official VS Code downloads page.

Installing the GitHub Copilot Extension

Start by launching your Visual Studio Code editor. Next, click on the Extensions tab. Use the search box to search for GitHub Copilot. Install and activate the extension (it has over 5 million downloads at the time of writing this):

Screenshot of GitHub Copilot VS Code extensionScreenshot of GitHub Copilot VS Code extensionScreenshot of GitHub Copilot VS Code extension
Screenshot of GitHub Copilot VS Code extension

Once fully active, a prompt will appear telling you to sign into GitHub.  Click the button to sign in. The authentication process will be quick because you’re already signed into GitHub and GitHub Copilot. If the process was successful, you’ll find the Copilot icon at the lower right hand corner of VS Code.

Note that GitHub Copilot is a paid service ($10/month at the time of writing) but there is a 30-day free trial.

Ask Copilot Technical Questions

While Copilot is known to be a coding assistant, you can ask it technical questions directly. This is perfect if you’re studying for a technical interview and want to quickly get answers to common interview questions.

To ask GitHub Copilot a question, put your question in a comment preceded by :q:

// q: What is a class in object-oriented programming?

Once you see a suggestion by Copilot (in grey color), click the tab key on your keyboard to accept it as your answer. The answer is preceded by :a. The tab key works on both Window and Mac computer. 

You can also ask specifically about one of those answers, (i.e. get more information about the answer). Copilot will figure out what you’re about to ask and autocomplete the question for you.

Using Copilot with HTML and CSS

Now let’s turn our attention to coding, starting with an HTML example. Copilot will help speed up the process of writing HTML. Let’s see how.

Create two HTML files in your project. The files should be named example1.html and example2.html. Next, open example1.html file in VS Code.

Start by typing the doctype declaration. When you click enter on your keyboard, Copilot already knows that the <html> tag is going to be the next obvious tag to add. So it suggests the tag (click tab to accept it)

Within that, Copilot suggests that you add the <head>, then <title>. It closes the <head>, and sure enough, <body> is suggested along with an <h1> tag and paragraph. 

If you want to generate an element, you simply describe the element you want to generate in a comment and press Tab. Here’s an example:

<!-- An h1 with inline blue heading -->

This will generate an <h1> element with blue text:

<h1 style="color:blue">This is a blue heading</h1>

You can also ask for a bulleted list with the following prompt:

<!-- Create a bulleted list -->

Here’s the result:

    <li>First item</li>
    <li>Second item</li>
    <li>Third item</li>

As per best practices, styles should always be in a separate stylesheet. Create a styles.css file in the same folder as the HTML files.

The following prompt will generate the link element that references the stylesheet. Write the prompt within the <head> tags in HTML:

<!-- Reference the stylesheet named style.css -->

This would be the output:

<link rel="stylesheet" type="text/css" href="style.css">

If the stylesheet file is inside another folder, just describe the directory structure in your prompt and Copilot will use the correct URL in the href.

Copilot Facilitating Bootstrap

With a simple “Add Bootstrap” prompt, Copilot will generate a link that references Bootstrap on the CDN. This is better than having to search the web looking for the up-to-date link to Bootstrap.

Copilot will also apply the Bootstrap classes on your elements. When you start a <div> element and hit the space bar, Copilot knows that you most likely want to add the alert and alert-success classes, so it suggests it. 

<div class="alert alert-success">
    <strong>Success!</strong> This alert box represents a successful or positive action

Just press Tab to accept the suggestion.

Regex Simplified With Copilot

If you’ve ever dealt with regex, you should know how confusing it can be to interpret the patterns. But with Copilot by your side, writing regular expressions can be a lot easier because you can ask Copilot to write a particular pattern for you.  

For example, if you wanted to a regular expression that matches a phone number, you’d the following prompt in a JavaScript file:

// Regex to match the phone number in the format (xxx) xxx-xxxx

If you hit Enter on your keyboard, Copilot will give you the regex you requested:

var phoneRegex = /^(d{3}) d{3}-d{4}$/;

Regex is commonly used for testing. Which is what the next section is all about.

Unit Testing Made Easy with Copilot

Copilot makes the unit testing super easy. Let’s see an example.

The following function takes a string, checks if it matches the regex, parses the first and last values, before joining the number based on the operator:

function calculator(str) {
  var regex = /^(d{3}) d{3}-d{4}$/;
  var match = str.match(regex)
  var num1 = parseInt(match[1])
  var num2 = parseInt(match[3])
  switch (match[2]) {
    case "+":  
      return num1 + num2;
    case "-":  
      return num1 - num2;
    case "*":  
      return num1 * num2;
    case "/":  
      return num1 / num2;
      return "Invalid operator";

Now you can tell copilot to test that function with console.log() using a prompt like this:

// test calculator function

Copilot will log each of the operations and give the output:

console.log("1+1") // 2
console.log("1-1") // 0
console.log("1*1") // 1
console.log("1/1") // 1

With the following prompt, Copilot will generate the coding for unit testing the function:

// create unit test for calculator function

var assert = require("asser")
assert.equal(calculator("1+1"), 2, "calculator can add")

It’ll not just generate the code, but also suggest other operations to test besides addition.

Choosing From Multiple Solutions by Copilot

So far we’ve seen Copilot give us suggestions. But always keep in mind that you don’t always have to choose the first suggestion. If you don’t like the first solution, you can always choose another one.

Consider the following prompt:

// Create a function that takes a string and returns it backwards

Start tabbing to see the suggestions. If you aren’t satisfied with the first one, hover on the text and navigate to the next solution (using the > icon). Once you arrive at an acceptable solution, you can then click the Accept button. 

Screenshot of function autocomplete by CopilotScreenshot of function autocomplete by CopilotScreenshot of function autocomplete by Copilot
Screenshot of function autocomplete by Copilot

Prompting Copilot With Multiple Conditions

When writing your Copilot prompt, you can specify multiple conditions in your prompt. This is quite useful if you want to write a complex program with different rules.

Let’s say you wanted to parse a list of expenses with some conditions. Inside the function, you’ll ask Copilot to do three things in your prompt (represented by the comment):

function parseExpenses(expenses) {
  /* Parse the list of expenses and return the array of
    triples (date, value, currency). Ignore the lines starting with //.
    Parse the date using Date()

Here we specified three conditions: parse the list, ignore comments, and parse the date. Hit Control + Enter your keyboard and pick the best solution from the suggestions. 

One of the suggestions when I tested this was the following:

   return expenses.split("n")
    .filter(line => !line.startsWith("//"))
    .map(line => line.split(","))
    .map(([date, value, currency]) => [new Date(date), Number(value), currency]);

That’s pretty good. But watch out, some of the suggestions that were suggested for me used line[0]=="/" to test which lines to ignore. This isn’t quite what we asked for!

It’s important to read the code generated by Copilot or any other AI tool carefully to make sure it matches what you expect.


In this tutorial, we looked at the basics of using GitHub Copilot. Just write your prompt in a comment and press Ctrl + Enter to see the suggestions. 

Leave a comment

Your email address will not be published.