Find helpful tutorials or share inspiring use cases on AI technology for higher education.

Shared content may not reflect the policies of Tilburg University on the use of AI. 

Guided Coding with GitHub Copilot Chat: A Quickstart for University Students

GitHub Copilot is known to most users for its ability to offer inline code suggestions which can be quickly inserted into your script with a simple press of the TAB key. In a previous article, we discussed strategies for refining these suggestions to better align with your coding style, understand your script’s higher-level goals, and adhere to best coding practices. Therefore, we highly recommend that article to every GitHub Copilot user. However, stopping there would be doing GitHub Copilot a great disservice, and indirectly, you, our reader. Thus, this article is about another feature the GitHub Copilot Chat.

In short GitHub Copilot Chat is an interactive chat interface where you can ask questions and get assistance with coding and programming tasks.

Copilot Chat: Interactive Assistance for Your Coding Needs

GitHub Copilot Chat is an interactive interface embedded in Visual Studio Code, designed to assist you with coding tasks. Whether you need help with specific code-related questions or want guidance on programming concepts, Copilot Chat is here to assist you without the need to trawl through documentation or search online forums.

While coding is often done with a programming language, you can get assistance based on natural language, similar to help from ChatGPT. However, you would need to upload your file and type in your question. With Copilot Chat, you can get assistance directly within your lines of code or use the main chat that understands your entire workspace. This means it can help with your file structure, assistance with connecting code between files, and offer many more opportunities. For GitHub users, it can also help with any Git-related issues.

Don’t have GitHub Copilot installed yet? This article will guide you through the installation process and help you set up VS Code to integrate your AI Code Assistant.

Using Copilot Chat

Copilot Chat is integrated into VS Code, making it easy to access and use directly within your programming environment. Here’s how you can use the chat tool inside VS Code:

  • Inline Chat: Initiate a chat directly from the editor for immediate support while coding.
  • Chat View: Open a dedicated chat window to keep an AI assistant at your side at all times.

Quick Chat: Inline Support Without Disruption

The advantage of inline code suggestions is that they provide code suggestions right where you are working. This prevents you from getting stuck and having to search for solutions yourself. While it is ideal for keeping your project moving, it is also important to understand what the code suggestion entails. After all, you want to know the answer yourself and understand what the code does when you encounter a similar problem in the future. We aim for AI-assisted learning. Therefore, GitHub Copilot has an inline chat feature: Quick Chat.

What is Quick Chat?

Quick Chat allows you to open a conversation with GitHub Copilot directly between your lines of code. Press ⌘ + I for Mac or CTRL + I for Windows users, and you will see a chat open directly in your code. You can ask questions like “Explain this piece of code” or “How do I add functionality to do X?” and receive immediate, context-aware responses. Copilot displays a preview of the changes, which you can either accept (⌘ + Enter or CTRL + Enter) or cancel (Escape). Alternatively, you can generate a new code suggestion.

The inline chat functionality is more convenient for quick fixes than opening up the main GitHub Copilot Chat’s side panel.

Here is an example of how to ask a specific question to Copilot. First, it provides a code suggestion, which we accept. Thereafter we can iterate by asking another question, for instance, about what a particular placeholder entails.

When you select specific lines, Copilot restricts your query to just those lines.

GitHub Copilot Chat: Expanding the Conversation

So far, we have discussed GitHub Copilot as an inline suggestion tool, ideal for on-the-go queries. However, the Chat view allows you to have a conversation with Copilot in a separate window. Ask Copilot Chat to give code suggestions, explain code, generate unit tests, and suggest code fixes. In this separate window, you can engage with Copilot to generate code suggestions, explain code segments, create unit tests, and even suggest code fixes.

How to use the Chat Function

  • Open the Chat View: Click the chat icon in the activity bar or use the shortcut ⌃ + ⌘ + I (Mac) / Ctrl + Alt + I (Windows/Linux).
  • Enter Your Prompt: Type your question or command into the input field, or choose from the suggested prompts.
  • Review Copilot’s Response: After pressing Enter, Copilot will respond in the chat view.
  • Inserting Code: If you find the suggested code useful, hover over it and click the “Insert at Cursor” icon, or press the copy button to manually insert it.

As you continue the conversation, Copilot keeps a record of your chat messages and responses. It also offers related follow-up questions or commands in its replies.

For assistance with GitHub Copilot and advice on interacting with Copilot Chat, type /help in the chat input box.

Using Keywords to Improve Chat Prompts

The last tip provides a nice opening into the next section: using keywords inside your chat prompt. You can use specific keywords to help Copilot better understand your prompt. Keywords consist of so-called Chat Participants, Slash Commands, and Chat Context. Let’s go through them one by one.

Chat Participants

Chat Participants are experts, each with their own specialty. They can assist you with their specific expertise. You can include a chat participant in your chat prompt by using the @ symbol followed by the name of the chat participant. The different Chat Participants are:

  • @workspace: The @workspace agent is aware of your entire workspace and can answer related questions. Use @workspace when you want Copilot to consider the structure of your project, how different parts of your code interact, or the design of a script and directory structure in your project.
Workspace Agent Prompts
  1. Understand Project Structure:
    • “@workspace, can you provide an overview of the folder structure in my project?”
    • “@workspace, what design patterns are used in this project?”
    • “@workspace, list all Python scripts that import the utils module.”
    • “@workspace, show me all the files where API_KEY is used.”
  2. Code Interaction:
    • “@workspace, where are the functions from helper_functions.py used in the project?”
    • “@workspace, is there any redundant code across different files?”
    • “@workspace, suggest a way to refactor the code in data_processing.py and data_cleaning.py to reduce duplication.”
  3. Error Tracking and Debugging:
    • “@workspace, help me trace the origin of this error in data_processing.py
  • @vscode: The @vscode agent understands Visual Studio Code commands and features. Use @vscode when you need assistance with Visual Studio Code.
VSCode Agent Prompts
  1. File Navigation and Management:
    • “@vscode, open the main.py file.”
    • “@vscode, show all open editors.”
    • “@vscode, close all files except the current one.”
    • “@vscode, create a new file named notes.txt in the root directory.”
    • “@vscode, compare script1.py with script2.py side by side.”
  2. Code Editing and Refactoring:
    • “@vscode, How can I rename all instances of the variable oldName to newName in the current file.”
    • “@vscode, format the entire document using the default formatter.”
    • “@vscode, remove all trailing spaces in this file.”
    • “@vscode, convert tabs to spaces in this file.”
  3. Extensions and Tools:
    • “@vscode, recommend an extension for Python”
    • “@vscode, install the Prettier extension for code formatting.”
    • “@vscode, list all installed extensions.”
    • “@vscode, disable all extensions for this workspace.”
  • @terminal: The @terminal agent understands the Visual Studio Code terminal and its contents. Use @terminal when you need help with creating or debugging terminal commands.
Terminal Agent Prompts
  1. Command Execution:
    • “@terminal, show all files in the current directory sorted by modification date.”
    • “@terminal, start a Python virtual environment.”
    • “@terminal, list all active processes related to Python.”
  2. Git Operations:
    • “@terminal, check the status of my Git repository.”
    • “@terminal, commit all changes with the message ‘Updated project files‘.”
    • “@terminal, create a new branch named updated.”
    • “@terminal, show the last 5 commits in the current branch.”
    • “@terminal, merge branch feature-xyz into main.”
  3. System and Environment Management:
    • “@terminal, check the current version of Python.”
    • “@terminal, display the current working directory.”
    • “@terminal, list all environment variables.”
    • “@terminal, find all files with the .log extension in the project directory.”
    • “@terminal, set a new environment variable API_KEY with a value of 12345.”

Slash-Commands

Slash commands are designed for common issues. In short, they are shortcuts to frequently used prompts. To use a slash command, type / in the chat prompt window, followed by a command.

For instance, the prompt “Create a new workspace with a Python Flask web app using Jinja and SQLite” indicates that you want to start a new project. Alternatively, you can use “@workspace /new Python Flask Jinja SQLite” which is more explicit, concise, and saves you time typing. Here, the /new slash command serves as a shortcut to quickly set up a new workspace.

Common Slash Commands
Slash-Commands Guide
Command Purpose How to Use It
/explain Explain the selected code Select the code you want to understand and type:
/explain what does the ... method do in this script?
/fix Suggest a solution for issues in the selected code Select the code with issues and type:
/fix suggest a solution for the problem in the ... function
/tests Generate unit tests for the selected code Select the code you want to test and type:
/tests generate unit tests for the ... function
/help Discover the features of Copilot Chat Type:
/help show all available commands and usage examples
/clear Start a new conversation Type:
/clear start a new session
/doc Add clear documentation to your code Select the code and type:
/doc generate documentation for the .... function
/generate Quickly generate code snippets Type:
/generate create a Python function to check if a number is prime
/optimize Improve the performance of your code Select the code you want to optimize and type:
/optimize improve the efficiency of the ... method
/new Scaffold code for a new project or file Type:
/new create a Django app with REST framework
/simplify Simplify complex code Select the code and type:
/simplify refactor this nested loop into a simpler format
@workspace /newNotebook (or /newNotebook) Create a new Jupyter Notebook Type:
/newNotebook
@vscode /runCommand Search or run a VS Code command Type:
/runCommand to search or run a specific VS Code command.

Chat Variables

Chat variables in GitHub Copilot are special placeholders that you can use to provide specific context to the AI. These enable you to reference specific files or other information in your prompt. Combine these with agents like @workspace, @vscode, and @terminal to easily pass all the necessary context to Copilot. To use a chat variable, type # in the chat prompt box, followed by the chat variable.

Chat variables include:

  • #file: Include a specific file as the context in the chat.
    • To reference a file, type # in the comment box, choose #file, and you will see a popup where you can select your file. You can also type #file_name.py in the comment box.
#file Prompts
  1. Debugging Assistance:
    • ” can debug the function in #file data_cleaning.py?”
    • “Review the code in #file assignment.py and suggest improvements for performance.”
  2. Code Explanation:
    • “Explain what the calculate_gpa() function does in #file student_records.py.”
    • “Can you help me understand the logic behind the if statement in #file final_project.py?”
  3. Refactoring Code:
    • “How can I refactor the code in #file data_analysis.py to be more efficient?”
    • “Can you suggest a way to simplify the nested loops in #file simulation.py?”
  • #git: Include information or retrieve information about the current Git repository. The #git variable lets you use information about your Git repository directly in Copilot, such as recent commits, the branch you’re working on, or other Git-related data.
#git Prompts
  • Version Control Queries:
    • “What were the changes made in the last commit in #git?”
    • “Can you summarize the differences between the current branch and the main branch in #git?”
    • “How does the code in #file project.py differ from the previous commit in #git?”
  • Branch Management:
    • “What branches are currently available in #git, and which one am I working on?”
    • “Create a summary of all recent commits in #git that affected #file final_project.py.”
  • #terminalLastCommand: This variable holds the last command executed in your active terminal in VS Code. It is useful when you want Copilot to consider the command you just ran, which can be particularly helpful for debugging. Additionally, Copilot can use this information to provide suggestions that align with your recent terminal activity.
#terminalLastCommand Prompts
  1. Command Execution and Debugging:
    • “Based on #terminalLastCommand, can you explain why the command didn’t execute as expected?”
    • “I just ran #terminalLastCommand. Can you help me write a script that automates this process?”
    • “Can you provide a breakdown of what happened during #terminalLastCommand and how to fix any issues?”

To see all available chat variables, type # in the chat prompt box.

Here’s an example of how to use chat variables, specifically #terminal. Keep in mind that you still need to run the terminal manually. Simply press the terminal button and hit Enter, and you’re good to go!

Concluding

Whether you are an advanced or beginner learner of a programming language, having an AI assistant like GitHub Copilot offers you a continuous learning opportunity. This AI tool is always ready to help you when you get stuck, provide an overview of your project directory through the @workspace agent and the @git agent, and explain new modules or packages you add to your arsenal. While the inline suggestions can sometimes take over and turn your code into a black box where you lose track of what you’re doing, this feature offers you interactive learning and coding assistance. With the example prompts provided, you hopefully already have an idea of the various directions you can take. Happy coding!