Guides for SE student projects »

This is a WIP community resource, containing contribution from community members.

VS Code - Using GitHub Copilot

GitHub Copilot is an AI Tool that is well-integrated with VS Code, because both tools are from the same parent company (Microsoft).

Pre-requisites

If you have not enabled Copilot in your GitHub account, follow the guide in the panel below:

Signing Up for GitHub Copilot

GitHub Copilot is an account-linked service that sits on top of your GitHub account. You need to enable it (via subscription or educational benefits) to use it. Given below are the steps:

  • Sign up for a GitHub Account, if you don't have one yet.
  • Fill in your billing information
  • Enable Two-Factor Authentication (2FA). You can skip this step if you have already done so.
    • Go to the Two-factor authentication page for your GitHub account.
    • Click on Enable two-factor authentication.
    • Open the Microsoft Authenticator App on your phone.
    • Tap the scan QR code button at the bottom right.
    • Scan the QR code displayed on the GitHub page.
    • Complete the remaining setup steps.
    • You should see the following page when 2FA is enabled.
  • Link your university email to GitHub (for Education benefits).
    • Go to the emails settings page in GitHub account.
    • Add your university-issued email under the Add email address field.
    • Verify your email using the link sent to your inbox.
    • Once verified, you will see your school email in the list of emails.
  • Apply for GitHub Education benefits
    • Go to the GitHub Education page.
    • Click on Start an application.
    • Select your role as Student.
    • Choose university as your school (e.g., National University of Singapore).
    • Follow the instructions and complete the application form.
    • Once your application is approved, it will show that you have a current student coupon applied.

Contributors: Norbert Loh (@NorbertLoh )


Installing GitHub Copilot in Visual Studio Code

  • Updating Visual Studio Code
    • Open Visual Studio Code.
    • Go to HelpCheck for Updates.
    • If updates are available, it should now say "Installing update…".
    • Once it finishes installing, go to HelpRestart to Update.
  • Install the Extension Pack for GitHub Copilot and the Extension Pack for GitHub Copilot Chat extension, to enable support for AI autocompletion.
    • Click on both of the extension links above.
    • Click on the Install button on the page that opens.
    • It should prompt you with Open Visual Studio Code? and click Open Visual Studio Code.
    • You will be redirected to the extension page in Visual Studio Code.
    • Click on Install to install the extension.
    • Make sure to install the Extension Pack for GitHub Copilot Chat as well with the same steps above.

Signing in to GitHub Copilot

  • Verify Prerequisites
  • Sign in to GitHub Copilot
    • Open Visual Studio Code.
    • Click on the GitHub Copilot icon at the top.
    • Click on Sign in in the pop up.
    • You will be redirected to GitHub login page, where you can login or select the account linked to GitHub Education.
    • It should prompt you with Open Visual Studio Code? and click Open Visual Studio Code.
    • Once signed in, the GitHub Copilot icon will now say "Toggle Chat" when hovered over.
    • Clicking on the GitHub Copilot icon will open the Copilot chat window.
  • Verifying the installation of GitHub Copilot
    • Open a new text file using FileNew File or / Ctrl+N | Cmd+N.
    • You should see an automatic suggestion appear in the empty text file.

Using GitHub Copilot in Visual Studio Code

Basic Code Completion

To get Copilot to complete code based on a comment:

  1. In a Java file, type a comment like // print Hello World inside the main method.
    public class Main {
        public static void main(String[] args) {
            // print Hello World
        }
    }
    
  2. GitHub Copilot will automatically suggest code to complete or implement the comment.

  3. Press Tab to accept the suggestion, or continue typing to refine it.
The more descriptive your comment or method signatures, the better Copilot can understand what you want to implement.

To disable automatic code completion,

  1. Look for the Copilot icon in the bottom right of vscode:

  2. Uncheck the necessary boxes (All files disables code completion for all files, while the checkbox below it disables code completion for the currently open file type.)

If you have access to Copilot Pro, you can also monitor your Copilot Premium Requests usage here.

Copilot Chat

  • Open the Copilot Chat using / Ctrl+Alt+I | Cmd+Alt+I or using the Copilot icon.

  • Ask something like: "Write a main method that calculates the factorial of N".

  • Copilot will generate a response in the chat window.

  • You can directly insert or copy code chunks using the buttons in the top right corner.

  • From left to right: Insert automatically into the current file, Insert at cursor, Copy to clipboard.

  • The Copilot Chat interface also contains a variety of useful features.

    1. You can toggle between 3 different modes, ask (ask a question and copilot generates an answer), edit (ask copilot to edit a select number of files) and agent (ask copilot to execute autonomously with a high level prompt). Find out more about the different modes here.
    2. By default, only the currently opened file will be included in the chat context (i.e. "visible" to Copilot). Include additional context by pressing the Add Context button, which will open up a window at the top that allows you to select files to add to Copilot Chat's context.
    3. You can choose between different AI models to use. If you have access to Copilot Pro (which comes with Github Education), you can select premium models.
    4. You can use mentions, by typing @ followed by the participant's name, or clicking on the @ icon. This allows you to invoke certain chat participants that are optimized to answer questions about their respective domains. One useful mention is @workspace, which answers questions about your entire repository.
You can find out more about managing context for Copilot Chat here.

Copilot Inline Chat

  • Go to the line or section where you want to ask Copilot for assistance.
  • Use the inline chat feature using the shortcut / Ctrl+I | Cmd+I.
  • Type your request such as "Add a print log here" and press Enter.
  • The generated lines of code will be highlighted in yellow, and you can insert them by clicking on accept.

Prompting Copilot in the Terminal

  1. Open a terminal in Visual Studio Code.
  2. Press Ctrl+I to bring up Copilot’s terminal prompt input.

Some common project-related prompts you might find useful:

  • Run the checkstyle
  • Compile and run the Java program
  • Run JUnit tests
  • Perform Git operations (possible but not recommended; if you are new to Git, we discourage using Git inside the IDE -- it is better for beginners to learn Git independent of other tools)

Contributors: Norbert Loh (@NorbertLoh ), Luoqi Xu (@Luoq1-Xu)