Humanity has come a long way in its technological journey. We have reached the cusp of an age in which the concepts we have collectively known as science fiction are quickly becoming science fact. The concept of artificial intelligence is highly anticipated yet controversial. Parts of society are apprehensive about its use, and perhaps for good reason. There are valid concerns about the safe and ethical use of this new technology. Regardless of any societal contention, the robots have arrived! This particular robot’s name is GitHub Copilot; if you’re a software developer, or learning to be one, it may be your new best friend.
What is GitHub Copilot?
GitHub Copilot is an AI code assistant tool that integrates with your IDE. It was developed by OpenAI in cooperation with GitHub. It uses the OpenAI Codex language model as its source to suggest generated code in real-time as you type based on the content of your editor’s buffer. There are currently plugins available for Visual Studio, Visual Studio Code, JetBrains, and Neovim. As a Vim user, I created this article to provide real examples of what these suggestions look like using the GitHub Copilot plugin for Neovim.
How does it work?
The OpenAI Codex parses public code sources in order to train its engine to transform natural language into runnable code. It’s currently in private beta, so if you’re interested, you can join their waitlist for access.
Getting Started with GitHub Copilot
Installing GitHub Copilot is a simple process that involves the following steps:
- Join the technical preview waitlist
- Download and install the plugin
- Activate the plugin
Join the Technical Preview Waitlist
At the time of this writing, GitHub Copilot is only available to a small group of developers. Before you can use it, you will need to apply for access to the technical preview in order to activate the plugin. Sign up for the wait list at https://github.com/features/copilot/signup.
Install the Plugin
GitHub Copilot has plugins available for the most common code editors. This article focuses on the Neovim plugin, but if you’d like to try out one of the others, read the documentation at https://github.com/github/copilot-docs, otherwise follow along to install the GitHub Copilot Neovim plugin.
Install the plugin by following the official documentation available in the plugin’s GitHub repository https://github.com/github/copilot.vim I highly recommend that you use a plugin manager such as vim-plug to install the GitHub Copilot plugin for Neovim, and all your other Neovim plugins as well.
:Copilot setupcommand, and you will be prompted to enter your activation code.
Agree to the telemetry terms and conditions, and you’re ready to start receiving AI-generated code directly in Neovim.
Configure the Plugin
By default, the following key bindings are configured:
||Accept the suggestion|
||Dismiss the current suggestion|
||Cycle to the next suggestion|
||Cycle to the previous suggestion|
I have the Alt key on my system reserved for another application, so I chose to remap these to
Ctrl-k respectively. I did this by adding the following to my Neovim config. The Neovim config file is typically located at
imap <silent> <C-j> <Plug>(copilot-next) imap <silent> <C-k> <Plug>(copilot-previous) imap <silent> <C-> <Plug>(copilot-dismiss)
As I was editing my Neovim config I was already getting suggestions! Line 137 in the image below is an auto-suggestion from the GitHub Copilot plugin.
Fun with Copilot
This fun example below is written using Copilot’s suggestions, with minimal help from me. I only wrote the initial comment to get it started.
Copilot gave us the basic outline of the steps to take, although, Copilot needs a little more help to figure out what we are doing. Adding the API URL should help. Let’s see what happens…
I do find it funny that Copilot would give us the GitHub API as its suggestion for the API URL. I really like how it was able to populate the import block for me based solely on the comments. Thanks Copilot!
Now I’m just going to use the suggestions for each line and see what Copilot can do on its own.
Pretty handy. This is essentially a valid program. Copilot understood we are working with user data and injected a
User type in there, but the definition doesn’t exist. Copilot will need to know the structure of our data to give us suggestions on parsing it. Let’s get that in there and then continue with the logic to parse the data.
But wait! There is another issue, if you caught it. The response from the GitHub API will return a single model, while Copilot set us up to parse an array; however, for the sake of this example, I’m going to leave it as is. I’m not planning on running this code anyway.
Neat! Copilot even gave us some fields to work with, though they aren’t the actual fields that are returned from the GitHub API – I know, I know: I’m expecting a lot from a robot. Next, let’s jump into some parsing logic. I’m just going to add some comments to ask Copilot to suggest some solutions for me and see what happens.
Well… I’m impressed. I can absolutely see how this can increase velocity for development. This is especially true for a language like Go that can require more explicit instructions to perform common operations, such as deleting an element from an array of models.
Copilot is the perfect name for this tool. I found that it does a terrific job at predicting my intentions as I’m authoring code. Copilot can become a developer’s best friend, and I’m certain that it will quickly change the way programmers write code. I can see how Copilot could be an incredible learning tool. It can be extremely beneficial for students, as well as experienced programmers that are learning the fundamentals of a new language.
Technology is exciting, and it makes our lives more convenient. Perhaps, however, we need to be aware of our dependency on the innovative tools we have available. GitHub Copilot is so useful and convenient that perhaps it’s possible for programmers to depend on it too much. Copilot can save us a lot of time referencing documentation, but this comes with the cost of not fully understanding critical concepts or having our coding muscles atrophy from lack of practice. One should make sure to always take the time to review the suggestions from Copilot, and to research why the solutions it provides work.
Links and Further Reading
If you’d like to learn more about Copilot, here is a list of links that can help you take a deeper dive:
- Official Copilot Documentation – https://github.com/github/copilot-docs
- Copilot Labs: VS Code Sidebar – https://next.github.com/projects/copilot-labs/
- OpenAI Codex White Paper – https://arxiv.org/abs/2107.03374
The post An introduction to GitHub Copilot Using the Plugin for Neovim appeared first on Big Nerd Ranch.