Post

GitHub | Using Copilot with Visual Studio Code

Using GitHub Copilot with Visual Studio Code

GitHub Copilot is an AI-powered pair programmer developed by GitHub and OpenAI. It helps developers write code faster by providing suggestions and auto-completions. In this guide, we’ll explore how to set up GitHub Copilot with Visual Studio Code and provide some examples of how to use it effectively.

Prerequisites

Before you start, ensure you have the following:

  • Visual Studio Code installed.
  • A GitHub Copilot subscription or access to the GitHub Copilot technical preview.

Installing GitHub Copilot

  1. Open Visual Studio Code.
  2. Go to the Extensions view by clicking on the square icon on the sidebar or using Ctrl+Shift+X.
  3. Search for “GitHub Copilot” and click “Install” next to the GitHub Copilot extension.
  4. Follow the on-screen instructions to set up GitHub Copilot.

Using GitHub Copilot

GitHub Copilot works alongside you as you write code, providing code completions and suggestions. Here are some key features:

Code Completions

As you type, GitHub Copilot suggests code completions based on what you’re writing. You can accept suggestions by pressing Tab or Enter.

Code Suggestions

GitHub Copilot offers helpful code suggestions when you’re stuck or need guidance. You can use the /// trigger to see suggestions for comments.

Examples

Example 1: Basic Code Completions

Let’s say you’re writing a Python function to calculate the square of a number. With GitHub Copilot installed, as you start typing, it provides code completions.

1
2
# Start typing a function
def calculate_square(number):

GitHub Copilot will suggest completing the function like this:

1
2
3
4
# Copilot suggestion
def calculate_square(number):
    """Calculate the square of a number."""
    return number ** 2

Simply accept the suggestion by pressing Tab, and you have a complete function to calculate the square of a number.

Example 2: Code Suggestions for Comments

GitHub Copilot is great at helping with code comments. If you’re documenting a function, use the /// trigger to get suggestions.

1
2
def divide_numbers(a, b):
    ///

GitHub Copilot will provide a comment suggestion like this:

1
2
def divide_numbers(a, b):
    """Divide two numbers and return the result."""

What Next?

By accepting the suggestion, you can quickly add descriptive comments to your code. GitHub Copilot is a powerful tool that can save you time and help you write high-quality code more efficiently. Experiment with it and let it assist you in various programming languages and scenarios.

This post is licensed under CC BY 4.0 by the author.