How to Use GitHub Copilot

GitHub Copilot is one of the most popular AI assistants for writing code. Find out how to communicate your intent to GitHub Copilot properly and write code faster, avoiding unnecessary trials and errors.

In this blog post, we explain how to make the most out of GitHub Copilot and even how to gain free access.

What is GitHub Copilot?

GitHub Copilot is an AI tool designed to assist developers in writing code. Branded as an AI pair programmer, it uses a vast database of open source code and machine learning algorithms to understand the context of the code and provides suggestions for lines or whole blocks of code as you type.

What sets GitHub Copilot apart from other similar solutions is its emphasis on user control. You are free to accept or reject its code suggestions, edit them manually, and browse through different options.

How does GitHub Copilot work?

GitHub Copilot’s suggestions are based on various contextual cues in your file, like function names, comments, docstrings, and the cursor’s position. This is especially helpful if you are working with unfamiliar frameworks and libraries. Copilot can quickly read through open source documentation and offer you relevant information and code snippets in seconds.

The tool supports all programming languages and can even work with plain text, creating suggestions based solely on the context within the file. One of the standout features of GitHub Copilot is its ability to understand both programming and natural human languages. However, its recommendations are not always perfect: some might not be practical or not make sense in certain contexts.

Also, keep in mind that GitHub Copilot doesn’t test the code it suggests. The program might not compile or run correctly, so it’s essential to review and test it thoroughly before use.

How to get started with GitHub Copilot

It’s really easy to start using GitHub Copilot, you just need to install the extension and learn a few keyboard shortcuts.

Install the extension

GitHub Copilot isn’t included by default in any text editors, so you’ll need to install it as an extension. For that, you can use:

  • Visual Studio Code Marketplace,
  • JetBrains Marketplace,
  • Neovim Plugin for GitHub Copilot.

Once installed, Copilot will ask you to log in to GitHub to authorize the plugin. After that, you’ll be redirected back to your editor. Check for the Copilot icon in your editor’s status panel to see if the installation has been successful.

Learn the keyboard shortcuts

The main keyboard shortcuts for GitHub Copilot vary slightly across macOS, Windows, and Linux platforms. So familiarize yourself with the following essential shortcuts:

Action Keys
Accept inline code suggestion Tab
Dismiss inline code suggestion Esc
Show next suggestion Use Alt + ] (or Option (⌥) + ] on macOS)
Show previous suggestion Use Alt + [ (or Option (⌥) + [ on macOS)
Trigger suggestion Use Alt + \ (or Option (⌥) + \ on macOS)
Open ten suggestions in a separate pane Ctrl + Enter

Keep this list handy as a quick reference while you work with GitHub Copilot and remember that shortcuts for different IDEs may vary.

For more beginner tips, watch this video:

Now your AI pair programmer is ready for work. In the following section, we’ll explain how to get the most of it.

GitHub Copilot best practices

GitHub Copilot has been trained on a vast array of code examples, enabling it to generate numerous predictions for the next line of code. The rule of thumb is that by providing more context around a specific line, you can help narrow down these predictions.

The general recommendations would be:

  • Create descriptive names for functions: Meaningful names help Copilot better understand your intentions and provide more accurate suggestions.
  • Guide Copilot with comments: By writing descriptive comments in your code, you can prompt the tool to generate specific code snippets or functions.
  • Explore different scenarios: Experiment with Copilot in various programming scenarios – from writing unit tests to exploring new libraries or frameworks. This approach can provide valuable insights.
  • Provide feedback: Take time to review and refine the suggestions, giving Copilot the right direction aligning with specific project requirements.

Now, let’s get into the specifics, keeping in mind that your final code serves two audiences: the computer that executes it and the programmers who read it. So, make sure your code is both accurate and comprehensible.

LLMs like GitHub Copilot are good at identifying and maintaining patterns. That’s why, to enhance Copilot’s effectiveness, it’s recommended to make your code more pattern-based, that is structure it in a way that follows consistent and recognizable patterns or blocks.

Open files

GitHub Copilot operates with limited “context windows,” meaning it cannot encompass an entire codebase in its view simultaneously. However, GitHub aims to offer Copilot some level of context. Therefore, if there are files open in your editor, GitHub might use their contents to inform Copilot’s suggestions. For example, if you’re writing tests, have the tested file open, or if you’re working on data processing, keep an example data file open. In some cases, multiple files may be relevant for context: one might illustrate the structure for a new file, while another provides essential data.

Comments

Incorporate comments at various levels. In high-level comments, explain the file’s purpose and its relation to the project. In line-level comments, specify the function and classes. The most effective comments are those that highlight how you resolve any ambiguities in code. For instance, in Python, a comment at the function level could specify whether a function processing list returns a new list or alters the existing one, as this greatly affects how the function is going to be written.

Imports

In many programming languages, it’s necessary to explicitly import standard or third-party libraries at the beginning of a file. This information is valuable for Copilot in determining the appropriate approach for code generation. For example, in Python, importing libraries like urllib3 and BeautifulSoup for web scraping will guide Copilot towards generating suitable code.

Names

Opt for descriptive names in your code, which provide Copilot with more context. For instance, if your code involves parsing a JSON file and storing the result, naming the variable simply as data might be too vague. If the result is a list of books, a more descriptive name like books suggests that the variable holds a sequence, with each element representing a book.

  • Use consistent variable naming conventions. Stick to a naming scheme that indicates how a variable is used, especially if it aligns with the conventions of the programming language. For variables holding array-like objects, adding an “s” suffix is a common practice. Also, Copilot will work better if it can understand your naming patterns (e.g. beginning getter functions with get).

Types

In languages where typing is optional, like Python or JavaScript, consider adding types, especially to function parameters and return values. This can significantly refine Copilot’s code suggestions.

Structured code

Organizing your code in a predictable and orderly manner benefits both readability and Copilot’s understanding of your code. Proper structuring applies not only to the code itself but also to related files.

# Creating an empty list

list1 = []


# asking number of elements to put in list
num = int(input("Enter number of elements in list: "))


# Iterating till num to append elements in list
for i in range(1, num + 1):
    ele = int(input("Enter elements: "))
    list1.append(ele)
Structured_code_GitHub_Copilot

This is particularly true when using popular frameworks. For instance, in the Django framework for Python, code is usually organized into specific files like models.py, views.py, urls.py, and a templates folder.

Privacy protection, licenсing and NDA when using GitHub Copilot

GitHub Copilot is trained on a vast amount of code, including public repositories on GitHub. This raises concerns about the use of copyrighted or licensed code. There’s a risk that Copilot might suggest snippets that are proprietary or under certain licenses, which could lead to intellectual property infringement if used without proper attribution or adherence to license terms.

There are also security concerns. GitHub Copilot requires careful handling when used in conjunction with confidential or NDA-protected code.

Here’s what you should be aware of:

  • Since the tool was trained on a dataset that includes public code from GitHub, it does not directly access or use private repositories or code that hasn’t been shared publicly. This means it doesn’t “learn” from or “remember” code it sees in private settings or under NDA.
  • If you’re working on code that’s under an NDA, Copilot itself does not “know” that the code is confidential. It will still provide suggestions based on its training.
  • The key concern with using Copilot on NDA-bound code is not that the tool will leak the code (since it doesn’t send your code back to a server or learn from it), but rather ensuring that the suggestions it provides do not inadvertently introduce snippets of code that may be proprietary or sensitive from its training dataset.

Bottom line: When working with confidential or NDA-protected code, you have to review any code suggestions provided by Copilot. It’s your responsibility as a developer to ensure that the use of such tools complies with their organization’s security policies and the terms of any NDAs.

For public code, you can use the code referencing feature which presents details about the matching code when you accept its suggestions. However, today it is only available Visual Studio Code.

Is GitHub Copilot free?

All good things come at a price. GitHub Copilot for Individuals costs $10 per month or $100 per year for each seat. The Business version is priced at $19 per seat, per month. Fortunately, each subscription comes with a 30-day free trial.

However, there’s good news. You can use the tool for free if you are a teacher, a student, or a maintainer of popular open-source projects.

You can find more details on GitHub Copilot’s FAQ page.

What are GitHub Copilot alternatives?

There exist several free and paid alternatives to GitHub Copilot. Here are some free options you may want to try:

Code Whisperer: This tool by Amazon is free for individual use. AWS toolkit extension, which not only provides the coding assistant features of Code Whisperer but also facilitates viewing, modifying, and deploying AWS resources. Code Whisperer supports a variety of programming languages.

IntelliCode: This is an open-source extension for Visual Studio and Visual Studio Code developed by Microsoft. It generates code suggestions customized to the current context and compatible with numerous languages and frameworks.

CodeGeex: It is driven by large code repositories of up to 20 programming languages and can convert code between different programming languages.

TabNine: This AI code assistant offers smart code completion, syntax highlighting, and error checking. It learns from open source datasets and is free for personal use.

In the video below, you’ll find a comparison of GitHub Copilot with two most popular alternatives: Google’s Duet AI and OpenAI-’s ChatGPT:

Conclusion

GitHub Copilot can significantly streamline your software development progress. To enhance your experience, focus on providing clear context through well-structured, smaller functions and descriptive comments. Experimenting with GitHub Copilot in various coding scenarios helps the tool continuously improve its suggestions related to your code, making it a better helper.

Useful resources

Delve deeper into the subject by exploring the materials we have gathered for you in this section.

Banner that links to Serokell Shop. You can buy stylish FP T-shirts there!
More from Serokell
haskell in production scarf thumbnailhaskell in production scarf thumbnail
detecting genetic disorders thumbnaildetecting genetic disorders thumbnail
Serokell’s Work on GHC: Dependent Types, Part 4Serokell’s Work on GHC: Dependent Types, Part 4