If you are new to programming or experienced in development you might have encountered GitHub Copilot. Copilot uses artificial intelligence to save you time when writing code. This virtual assistant system suggests coding blocks and completes your lines while writing entire functional sections. This article shows you how to use Copilot within VS Code by following simple instructions. Our guide will start basic to help both newcomers and experienced users learn Copilot through clear examples and basic code samples.
What is GitHub Copilot?
GitHub Copilot is a code completion tool that GitHub built with OpenAI technology. It uses machine learning to study your code and gives recommendations as you type. It helps you work effectively both with basic functions and challenging code problems.
Prerequisites
- Visual Studio Code (VS Code): This is the code editor where Copilot works. You can download it from here.
- GitHub Account: We will need a GitHub account to use Copilot. If you don’t have one, sign up here.
Install the GitHub Copilot Extension
Visual Studio Code requires the installation of Copilot’s extension before we can use it. Here’s how we can do that:
- Open Visual Studio Code.
- Access the Extensions Marketplace through two methods: Click the Extensions icon in the left Activity Bar or use the keyboard shortcut Control + Shift + X (Windows/Linux users) or Command + Shift + X (macOS).
- Type GitHub Copilot in the search field.
- After finding the GitHub Copilot extension in results click the Install button.
- Install Copilot extension.
- After a successful installation, the Copilot extension is ready to use.
Sign In to GitHub for GitHub Copilot
Our next step after adding the Copilot extension is to authenticate our GitHub account. Here’s how:
- Click the Copilot icon that appears on the left side of your screen window.
- The system displays a login screen to connect with GitHub. Click Sign In.
- Click on the on-screen prompts to confirm our GitHub account login. During this process we need to open GitHub and let VS Code use our account.
- The Copilot tool turns on automatically when we log into our account. Our developer work environment is ready for Copilot use. As we type in the code editor Copilot offers smart suggestions to help us write code more quickly.
GitHub Copilot Suggestions
If Copilot suggestions do not appear on it’s own, we can trigger them using Ctrl+Space or Alt+Enter. Here’s how we can interact with Copilot:
- Accept a Suggestion: Press the Tab key to choose the suggestion.
- Reject a Suggestion: To reject the suggestion simply press Esc.
- Press Alt + ] (Windows/Linux) or Option + ] (macOS) to cycle through alternative suggestions.
How to Use GitHub Copilot in Visual Studio Code
Now that Copilot is set up, let’s explore how it works with some beginner-friendly examples.
Example 1:
Suppose we want to write a function that adds two numbers. Here’s how Copilot can assist us:
- Start typing the following code:
def add_numbers(a, b):
- The moment you start typing the function name, Copilot will suggest the rest of the code. We can press Tab to accept the suggestion.
- Copilot finished creating our function to save us labor and time.
Example 2:
Let’s try another example. Suppose we want to create a list of squares.
# Create a list of squares
squares = [
Copilot will suggest the next line.
Example 3:
Copilot helps us find and solve problems in our programming code. When our code produces an error it can provide us with a suggested solution.
Consider the following code with an error:
def divide(a, b):
return a / b
result = divide(10, 0)
print(result
if b == 0:
Copilot might suggest adding a check to avoid division by zero:
Our code quality improves when Copilot provides assistance. Now, you might wonder what those little stars out there are. Clicking on them opens a quick fix window, allowing you to add Copilot chat assistance within your code.
Limitations of GitHub Copilot
While Copilot is a powerful tool, it is important to be aware of its limitations:
- Not Always Accurate: Copilot generates output that relies on current codebases and may not produce the most refined secure options.
- Limited Context Awareness: Copilot needs a complete understanding of our project to deliver appropriate recommendations.
- Dependency on the Internet: Copilot needs an ongoing Internet connection to operate.
We need to verify suggestions by checking them against our projects before using them.
Conclusion
GitHub Copilot brings powerful new features to help us work better with our code. Copilot saves us time by handling routine tasks and suggesting smart ideas so we can tackle difficult problems and produce better code. Using this guide helps us easily add Copilot to our work routine and make the most of its features. Copilot serves to support our work rather than take our place.
Since you seem interested in extensions, click here to learn about the best Chrome extensions to speed up your work.