Using Copilot to write code within VS Code

Using GitHub Copilot within GitHub is a powerful way to enhance productivity and accelerate coding. Copilot works as an AI-powered coding assistant that suggests code snippets and entire functions based on the context of your project.

Here’s a step-by-step guide on how to use GitHub Copilot when writing code within GitHub:


1. Set Up GitHub Copilot

Step 1.1: Enable GitHub Copilot

  1. Sign In to GitHub:
  2. Check Copilot Subscription:
    • Ensure you have an active GitHub Copilot subscription:
      • Go to Settings > GitHub Copilot.
      • If not subscribed, start a free trial or purchase a subscription.

Step 1.2: Install VS Code (for local integration)

While Copilot doesn’t directly integrate into the GitHub web interface, it works seamlessly within VS Code when connected to your GitHub account. Install VS Code by visiting https://code.visualstudio.com/.

Step 1.3: Install GitHub Copilot Extension

  1. Open VS Code.
  2. Go to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X).
  3. Search for GitHub Copilot.
  4. Click Install.

2. Use GitHub Copilot While Writing Code

Step 2.1: Open or Clone a Repository

  1. Clone a repository from GitHub to your local machine:
    git clone git@github.com:username/repository.git cd repository
  2. Open the repository in VS Code:
    code .

Step 2.2: Write or Edit Code

  1. Start a New File: Create a new file (e.g., main.py).
  2. Type Comments or Partial Code: Copilot generates suggestions based on comments or incomplete code. For example:
    • Type a comment like:
      # Function to calculate the factorial of a number
    • Copilot will suggest:p
      def factorial(n): if n == 0: return 1 return n * factorial(n - 1)
  3. Accept Suggestions:
    • Press Tab to accept a suggestion.
    • Use Arrow Keys to navigate through multiple suggestions.
  4. Refine Code:
    • Edit the suggested code if needed.
    • Request additional suggestions by typing more comments or code.

Step 2.3: Use Context

Copilot understands the context of your code, including functions, variables, and imported libraries. It adjusts its suggestions based on the surrounding code.


3. Examples of Using Copilot

Example 1: Generate Functions

Type a descriptive comment, and Copilot will suggest a function:

# Function to find the largest number in a list

Copilot suggests:

def find_largest_number(numbers):
if not numbers:
return None
return max(numbers)

Example 2: Generate Repeated Code

Start typing a loop or pattern, and Copilot will autocomplete it:

for i in range(10):

Copilot completes:

for i in range(10):
print(f"Iteration {i}")

Example 3: Code with Libraries

Import a library, and Copilot suggests usage:

import pandas as pd

Copilot suggests:

data = pd.read_csv('file.csv')
print(data.head())

4. Use GitHub Copilot with GitHub Actions or Web Development

Write GitHub Actions Workflows

Create or edit a YAML file for GitHub Actions:

# A workflow to build and test Python code

Copilot suggests:

name: Python CI

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: pytest

Frontend Development

For a React project, type:

// Create a React component for a button

Copilot suggests:

import React from 'react';

function Button({ label, onClick }) {
return <button onClick={onClick}>{label}</button>;
}

export default Button;

5. Debugging and Testing with Copilot

1. Use Copilot to Write Test Cases: Type:

import unittest
from main import factorial

class TestFactorial(unittest.TestCase):
    def test_factorial(self):
        self.assertEqual(factorial(0), 1)
        self.assertEqual(factorial(5), 120)

if __name__ == '__main__':
    unittest.main()

2. Fix Bugs with Copilot:

  • Highlight buggy code.
  • Add a comment describing the expected behavior.
  • Copilot will suggest fixes.

6. Commit and Push Code to GitHub

Step 6.1: Stage and Commit Changes

  1. In the terminal:
    git add . git commit -m "Added factorial function and tests"

Step 6.2: Push to GitHub

  1. Push your code:
    git push origin main

Step 6.3: Review Code in GitHub

  1. Open your GitHub repository to review the changes.

Tips for Effective Copilot Use

  1. Be Specific: Clear comments lead to better suggestions.
  2. Iterate: Refine Copilot’s suggestions for optimal results.
  3. Context Matters: Keep related code together to help Copilot understand your intentions.

GitHub Copilot is a powerful tool that accelerates development and simplifies tasks. By combining it with Git and GitHub, students can build, document, and manage projects efficiently.

Scroll to Top