The Ultimate User Manual for GitHub Copilot
Writing code is often a tedious and time-consuming task. Modern developers are always looking for new ways to improve programming productivity, accuracy, and efficiency. Automated code generation tools like GitHub Copilot can help achieve this goal.
What is GitHub Copilot?
GitHub Copilot is one of the latest tools launched by GitHub. As an "AI pair programmer," Copilot uses artificial intelligence to automatically generate code in your editor. It can be used as an extension in Visual Studio Code, JetBrains IDE suite, Neovim, and more.
However, GitHub Copilot is not just an auto-complete solution. Based on the context clues of the code you're writing, Copilot suggests lines or even entire functions. This makes it faster and easier for developers to create tests, explore APIs, and solve problems without constantly searching for other answers.
When you start using the GitHub Copilot plugin, the tool adapts to the way you write code.
Copilot is very fast and seamlessly integrates with the workflow you use when writing code. Once you get familiar with it, you can complete the required code with just one click on the keyboard.
Unlike similar solutions on the market, GitHub Copilot gives you complete control - hence its name. Accept or reject suggested code, manually edit suggestions, and cycle through alternative suggestions are all up to the user. As the tool adapts to your coding style, future prompts will become increasingly smarter.
Example of Using GitHub Copilot
GitHub Copilot is powered by OpenAI Codex, which generates suggested source code based on information within the file, such as function names, code comments, docstrings, file names, cursor position, etc. Based on this information, Copilot provides code snippets that developers can accept with a simple press of the Tab key.
Because the AI suggestions come from open-source projects in public GitHub repositories, it analyzes the information contained therein and then tries to find the best possible solution based on what you are currently writing.
GitHub Copilot stands out from other solutions thanks to its ability to understand natural language, including programming and human languages. However, it's not perfect and some suggestions may not be effective or make sense. The tool doesn't test the code it suggests, so careful review and testing are still necessary.
To make the most of GitHub Copilot, break your code into smaller functionalities and use meaningful function parameter names. Copilot can be particularly helpful for developers using unfamiliar frameworks and libraries, as it can navigate open-source documentation for them.
While GitHub Copilot offers a vast range of suggestions and solutions, it's important to remember that it's just a tool and can't replace human developers. The responsibility to accept suggestions and make modifications still lies with the developer.
Explore different examples where Copilot can be used to better understand its functionality and versatility.
Example 1: Converting comments to code
One of the coolest features of GitHub Copilot is that it can convert your comments into code. Simply create a comment that describes the desired logic, and Copilot will automatically generate suggestions for you.
In this case, the comment simply says "List all GitHub repository names for the organization."
Copilot immediately generates a suggestion. If you were writing this code, you could simply accept it by pressing the Tab key. As you can see, this comment is written in plain English. GitHub Copilot still understands the intent and provides appropriate suggestions.
This ties in with something I mentioned earlier - always write good comments and docstrings. If your comments are written in unnatural language, Copilot may have a hard time understanding the proper intent.
Example 2: Auto-filling repetitive code
GitHub Copilot is an ideal way for developers to speed up writing repetitive code. If you're writing a lot of boilerplate code, simply input a few pattern examples and Copilot will handle the rest.
In this example, constant variables are started in seconds. Once the const that multiplies minutes by seconds is displayed on the second line, Copilot recognizes the pattern and automatically completes the code, including hours, days, weeks, months, and years.
These five lines of additional code can be written with just one click. In terms of scale, this will save a lot of programming time, especially for larger blocks.
Example 3: Running Tests
As mentioned earlier, GitHub Copilot doesn't actually test the code it suggests. However, you can use it to suggest tests that match your code implementation. This is a good way to quickly import a testing unit package.
Here's an example of generating tests from plain English comments:
You still need to verify if the code is reasonable, but this is a faster option than completing the code on your own.
Example 4: Navigating in an unfamiliar field
This particular use case can be said to be Copilot's best feature. It's an excellent way for developers to navigate in unfamiliar languages or frameworks.
For example, suppose you want to draw a scatter plot. Depending on the programming language you're using, the way to write this code will vary greatly. Here's an example of the code in Python:
Even if you have experience and proficiency in Python programming, this autocomplete feature can still save you time.
Without Copilot, you would be forced to manually search public repositories for examples, or you may use resources like Stack Overflow to find answers. However, both of these alternative solutions are tedious and time-consuming.
Experienced developers enjoy using Copilot when dealing with unfamiliar languages. Even if Copilot's suggestions are not perfect, it can still get the basic syntax right. When it comes to common idioms, library functions, and more, it also guides you in the right direction. Therefore, Copilot can even be used as a self-learning tool for programmers.
Example 5: Creating an Application Entirely with Copilot
In addition to the numerous examples of Copilot's applications, I was looking for a real success story of something created with Copilot. I found an excellent case study written by a software engineer from the UK on LogRocket.
Programmer Evgeny Klimenchenko decided to create a simple test application to see if Copilot could handle the project. The application is a random quote generator that also displays the emotion of the quote.
To truly test Copilot, Klimenchenko told himself that he would not search for solutions on Google or Stack Overflow. He would rely entirely on Copilot's suggestions. He also would not write any new code, but allowed himself to write variables, comments, function names, and edit suggestions.
Within a week, Copilot helped Klimenchenko create a simple quote generator application. It was very basic and didn't have any specific use. But this case study proved that Copilot's functionality is as advertised.
How to Get Started with GitHub Copilot?
If this is your first time using GitHub Copilot and you're unsure of how to proceed, then you've come to the right place. The following steps will not only tell you how to use GitHub Copilot, but will also set the foundation for your success.
Step 1: Have a clear idea of what you want to accomplish with GitHub Copilot
Don't just blindly dive into GitHub Copilot or approach it with a "let's see what happens" attitude. This may prevent you from fully utilizing the tool's functionality.
For example, you may decide to strictly use GitHub Copilot for automatically filling in boilerplate code. Everything else can still be written by you as usual. But when it comes to repetitive lines that can be auto-completed, you can take advantage of Copilot.
Or perhaps the complete opposite. Instead of using Copilot to help with routine programming tasks, you may want to run experiments similar to the case studies we discussed earlier.
Many developers have used GitHub Copilot to their advantage when working with an unfamiliar programming language. It will aid in their understanding of syntax and basic knowledge of library functions. Once you have determined how you plan to use Copilot in your next project, the rest of the steps will become much easier.
Step 2: Install GitHub Copilot Extension
GitHub Copilot is not provided with any editor by default. Therefore, you need to add the extension before you start using it.
You can install Copilot from the following channels, depending on your preferred editor:
- GitHub Copilot - Visual Studio Code Marketplace
- GitHub Copilot - JetBrains Marketplace
- Plugin for Neovim to use GitHub Copilot
Personally, I think the GitHub Copilot extension works best in Visual Studio Code. This is because Visual Studio Code also works in GitHub Codespaces.
After installing the extension, Copilot will prompt you to authorize the plugin by logging in to GitHub.
If the extension is correctly installed, you should see the Copilot icon on your status panel.
Step 3: Learn the Keyboard Shortcuts of GitHub Copilot
You should be familiar with the common keyboard shortcuts of GitHub Copilot. They may vary slightly depending on whether you are using macOS, Windows, or Linux.
- Accept inline code suggestion: Tab
- Reject inline code suggestion: Esc
- Show next suggestion: Alt + ] or Option (⌥) + ]
- Show previous suggestion: Alt + [ or Option (⌥) + [
- Trigger suggestion: Alt + \ or Option (⌥) + \
- Open ten suggestions in a separate pane: Ctrl + Enter
Step 4: Start writing your code
Now you can start working as usual.
During the writing process, you will see GitHub Copilot automatically suggest autocompletion options based on context. Whether to accept or reject these options depends on you.
If you don't like what Copilot provides, you can always look at other suggestions to see if they are more relevant. Copilot definitely takes some time to adapt, but you will get more and more used to it.
Step 5: Edit and test your code
As mentioned earlier, Copilot is not perfect. Therefore, you cannot just look at the surface value of the suggestions and think everything is perfect.
You may need to make some minor edits to the code. As usual, you should always run tests before submitting code to your project.
GitHub benchmarked the accuracy of Copilot by reviewing a set of Python functions in open source repositories. They removed the function body and prompted Copilot to fill it in. Copilot was correct 43% of the time on the first try. When Copilot was allowed to try 10 times, the code was correct 57% of the time.
If this benchmark reflects your performance when using Copilot, you are likely to need to make some minor modifications to the suggestions.
Overall, GitHub Copilot is a valuable addition to any developer's toolkit and worth exploring for those looking to improve their coding workflow.