How to setup and use Github Copilot on Vscode on Linux

Endrit Qerreti

Endrit Qerreti

Github Copilot is like ChatGPT, but for coding generation only. Github Copilot is basically an AI programmer that helps you writing code and solve different problems you encounter when coding.

If you are working on a project and you get stuck, Github Copilot can help you with suggestions what a solution could be, this happens to all of us, as one can't remember everything. The great thing about Github Copilot is that, it suggests and writes code directly in the vscode so you don't have to leave vscode when using it.

In this tutorial, you will learn how to setup Github Copilot on Visual Studio Code.

Install Github Copilot on Vscode

Installing Github Copilot in vscode is easy, all you have to do basically is to install its extension.

Launch Vscode , and go to Extensions, you can go to extensions section by clicking the extensions button at the sidebar, or simply by using the shortcut : CTRL + SHIFT +X

Now, once you are in the extension section, type "Github Copilot" in the search box and press enter

And you should see Github Copilot extension listed below in the results

Now, you can install the extension directly from the results page by clicking the Install button as shown in the image above. However, i'd recommend installing it from its dedicated page because you can read the documentation how the extension works, and other stuff like features or changelogs.

Click the Install button to install Github Copilot's extension on vscode

Once Github Copilot has been installed on your system, you will see the following pop up as shown in image below. Now, you need to click to "Sign in to GitHub" button

💡

Note: The sign in pop up appears only if you are not logged in on Github when installing Github Copilot extension.

Next, click to "Allow" on this prompt

And complete the Authorization by clicking to "Authorize Visual-Studio-Code" button

Once done, it should redirect you to this page, on this prompt you need to click to "Open xdg-open" button to allow vscode.dev to launch vscode.

Finally, click to "Open" to complete the verification

If you see the following pop up "Github Copilot could not connect to server. Extension activation failed" When already logged in to Github

This happens because Github Copilot is not activated for your account. If you already have an account on Github Copilot, check if the subscription is valid and active for your account, and then reload vscode.

Use Github Copilot

To use Github Copilot, all you have to do is write the question on vscode. We are going to show you some examples of how you can use Github Copilot on your projects.

For example, let's say you want Github Copilot to help you import the required modules for a python web project, to do this, you can use the command below, press enter to execute the command

# import modules required for python web framework

💡

Note: You can also use the command above without commenting it with #, or by using // instead, or nothing at all, but I wouldn't recommend just plain text because when the code is generated by Github Copilot, you will need to manually delete the line that you wrote. It also depends on the coding language that you are coding with, for example if you are coding in python then using # is the right way to ask Github Copilot, and when using for example javascript you can use // so it doesn't break the code.

Next, you should see a suggested solution by Github Copilot, as shown in image below

Now, you need to press TAB to accept the suggestion, and the code will get written as suggested by Github Copilot

Let's see now how Github Copilot works when coding. We are going to declare three variables

Name = 'John'
Age = '25'
Weight = '80'
Address = 'Location'

And then we will ask Github Copilot to print all the variables into one line, by using the query

// print all variables above in one line

And this is what Github Copilot suggested

print(Name, Age, Weight, Address)

We asked to print only the value of the variable, let's see if we can print variables including the variable name

// print all variables above in one line, including the variable name

And this is what Github Copilot came with

The secret to make Github Copilot write the code that you are looking for, is you need to know what you want. What I mean is that, you need to describe in details what you are looking for.

For example, let's say I want to replace the variable name with another variable called name2 from the code below

Name = "John"
Age = "25"
Weight = "80"
Address = "Location"

And I ask Github Copilot to do this by using the following query

// replace Name with Name2

And this is what Github Copilot suggested based on the query above

It did not do what we were looking for, why? Well it's simple, because it doesn't understand what Name and Name2 is

Let's try again, using another query

This time we are going to specify that we want to convert the variable Name to another variable called Name2

// convert the variable Name to another variable called Name2 

And this is what Copilot suggested

Still it did not do what we were asking for, let's try again using a better described query

// rename Name variable to Name2

And.. it worked

Github Copilot is a great tool, there's no doubt about that, and the more you use it, the better results you will get from it.

What else can you do with Github Copilot?

You can ask Github Copilot to rewrite your code, do you have a piece of code that you want rewritten because it doesn't look that good, or you just want to see other ways how to rewrite it? You can, simply go to Github Copilot Lab by clicking the small icon at the sidebar of vscode, then choose "Readable" under brushes section

💡

Note: You must have Github Copilot Labs extension installed on your system to use this feature.

Install Github Copilot Labs

You can install Github Copilot Labs from the extensions just like you installed Github Copilot, simply search for "Github Copilot Labs" on the extensions section, and click the install button to install it.

Once Github Copilot Labs has been installed on your system, you should see the icon at the sidebar.

Github Copilot Labs can do a lot more than just rewriting, it can also explain code, fix bugs, debug your code, clean your code, translate your code into another language and more.

For example, let's see how you can ask Github Copilot Labs to explain a piece of code

Go to Github Copilot Labs > Select the code that you want explained and click the "Ask Copilot" button as shown in the image below

And you should see the explanation

How about translating a piece of code from python to PHP? We have the following python code and we are going to translate it to PHP

def myname():
    name = "myname"

    print(name)


myname()

Go to Github Copilot Labs, select the piece of code that you want to translate, next choose the language that you want to translate it into, finally click the "Ask Copilot" button to translate your code

And you should see the translated code

How to fix bugs using Github Copilot Labs

Fixing bugs with Github Copilot Labs is easy. All you have to do is select the piece of function or code if you suspect that it has more than one bug, and use the "Fix bug" feature on Github Copilot Labs.

For example, we are going to fix a bug in the following python code using only Github Copilot Labs

def myname():
    name1 = "myname"

    print(name)


myname()

The bug here is that we are printing the wrong variable, so to fix this bug, select the whole code and click to "FIX BUG" button

And you should see the new code with no bugs

How to clean a messy code

Another great feature that Github Copilot Labs has is the "Clean" code feature, which allows you to clean code that it's not being used. It basically removes unnecessary code from your code, instead of cleaning your code manually, you can do it in one click by using the clean code feature on Github Copilot labs.

For example, the following code has 3 unnecessary variables

def myname():
    name = "myname"
    name1 = "name1"
    name2 = "name2"
    name3 = "name3"

    print(name)


myname()

To clean the code above, we need to select the whole code, and then click the "CLEAN" button

And the code is clean now, the three variables that were not being used are removed

Conclusion

In this tutorial, you learned how to Install, configure and how to use Github Copilot Labs on Visual studio code, step by step. We also went through some useful examples of what you can do with Github Copilot and Copilot labs.