If you've ever used the terminal, you know the struggle of trying to remember a command, the fear of breaking your system with a random command you found on the interwebs or the frustration of trying to find the right command to do what you want.
That struggle is lightened today, with GitHub Copilot in the CLI! ππΌ
GitHub Copilot in the CLI provides a chat-like interface in the terminal that allows you to ask questions about the command line.
It is designed to help you with general shell commands, git commands and gh cli commands. This post aims to get you up and running with installing, and using copilot in the cli.
Installation
To install copilot in the cli, you must first install GitHub CLI and complete authentication in an OAuth browser window. If you're not familiar with the GitHub CLI, learn how to use it by reading this blog post!
Since I'm on macOS, I used homebrew as my package manager:
brew install gh
gh auth login
You also need to have an active GitHub Copilot subscription to use copilot in the cli.
Once the gh cli
is installed, and you have a copilot subscription, you can install the extension by running:
gh extension install github/gh-copilot
And you are ready to go! π
Available Commands
I recommend running the help command to understand how to interact with copilot in the cli. When you run gh copilot --help
you will see the following output:
gh copilot
Your AI command line copilot.
Usage:
copilot [command]
Available Commands:
config Configure options
explain Explain a command
suggest Suggest a command
Flags:
-h, --help help for copilot
-v, --version version for copilot
Use "copilot [command] --help" for more information about a command.
There are two primary commands you can use to interact with copilot in the cli: gh copilot explain
and gh copilot suggest
.
Create an Alias π‘
π‘ Hot tip! creating an alias will make using copilot in the cli a part of your regular workflow a lot easier.
After installation, the next thing I advise you to do is to create an alias. This will make interfacing with copilot in the cli a lot smoother because it saves you some keystrokes and you can get help faster.
Since I'm using zsh, I ran the following commands to create aliases:
alias copilot='gh copilot' ; echo 'alias copilot="gh copilot"' >> ~/.zshrc && source ~/.zshrc
alias gcs='gh copilot suggest' ; echo 'alias gcs="gh copilot suggest"' >> ~/.zshrc && source ~/.zshrc
alias gce='gh copilot explain' ; echo 'alias gce="gh copilot explain"' >> ~/.zshrc && source ~/.zshrc
Now, let's get into what these commands mean and a few examples on how to use them.
π‘ Remember, I have an alias so I'll be using
gce
orgcs
instead ofgh copilot explain
orgh copilot suggest
.
Explain Command π€
There are two ways you can use the copilot cli explain command:
- You can run the explain command with no arguments and it will prompt you to enter a command to explain.
gh copilot explain
- You can run the explain command and the command you want explained at the same time:
gh copilot explain 'your query'
You can also run the explain command with the -h
flag to get more information:
gh copilot explain -h
Suggest Command π€
There are 3 ways you can interact with copilot in the cli to get a suggestion:
- You can run the suggest command and follow the on screen prompts:
gh copilot suggest
- You can run the suggest command and your query at the same time, then select the type of command:
gh copilot suggest 'command you want to run'
- You can run the suggest command, your query and the type of command at the same time:
gh copilot suggest 'command you want to run' -t 'type of command'
The -t
flag means type of command and can be one of the following:
- git
- gh
- shell
Revising Suggested Commands
Sometimes, the suggestion you get from copilot in the cli is not exactly what you want. You can revise the command by selecting the Revise command
option when you receive a suggestion and give copilot about more detail about what you want:
π‘ Hot tip! Copilot in the CLI is not there to simply give you the suggested you want. Remember, it is there as your co-pilot, so if the output is not quite what you expected, edit the suggestion or ask for a revision until you get what you need.
Explaining Suggested Commands
When you get a suggestion from copilot in the cli, you can ask copilot to explain the command by selecting the Explain command
option:
Copying Suggested Commands
When you get a suggestion from copilot in the cli, you can copy the provided response and run it in your terminal.
Rating Commands πππππ
When you get a suggestion from copilot in the cli, you can rate the command by selecting the Rate command
option, and selecting whether or not it was helpful:
Exiting the prompt flow window πͺ
You can exit the prompt flow window by selecting the Exit
option:
Examples of using Copilot in the CLI
Now let's take a look at some examples. ππΌ
1. Explain a dangerous command β οΈ
So, I saw this command online and was curious about what it did so I can ask:
gh copilot explain chmod -r 777 /
π‘ Hot tip! When asking copilot to explain commands that have special characters, enclose the command in a string so copilot can understand it better.
As you see, this command is actually very dangerous and can cause you to lose all your data. What I love about the explanation I received was that it not only told me what the command does, but it also told me why it was dangerous.
π¨ NEVER RUN THIS COMMAND ON YOUR MACHINE π¨
2. Delete a git branch locally and remotely
In the visual example below, I first list the branches in my local repo with the command git branch
then I list the branches in my remote repo with the command git branch -r
. I then ask copilot in the cli for a suggestion:
gh copilot suggest 'delete a git branch locally and remotely' -t git
I was provided with the following suggestion:
git push origin --delete <branch_name>
git branch -D <branch_name>
I ran the suggested commands, then listed the branches in my local repo and remote repo again to confirm that the branch was deleted. Thankfully, it was. ππΌ
View all open issues in a repo
You can also ask copilot in the cli for gh
commands. For example, if you wanted to view all open issues in a repo, you can ask copilot in the cli for a suggestion:
gh copilot suggest 'view all open issues' -t gh
I was provided with the following suggestion:
gh issue list --state='open'
I ran the command and was provided with a list of all open issues in the repo I was currently in. See the flow in the visual example below:
3. Kill processes with open files that have been deleted
Copilot in the CLI can also be useful for administrative tasks in the terminal. For example, if you wanted to kill processes with open files that have been deleted, you can ask copilot in the cli for a suggestion:
gh copilot suggest 'kill processes with open files that have been deleted' -t shell
I was provided with the following suggestion:
lsof | grep '(deleted)' | awk '{print $2}' | sort -u | xargs -r kill -9
I asked copilot to explain the suggestion because I wasn't very familiar with the xargs
command. See more in the video example below:
Once I ran the suggested command, I was able to run the command lsof | grep '(deleted)' | awk '{print $2}' | sort -u
to see the processes that were killed.
4. Exit Vim π
This would not be a terminal demo if I didn't ask this one question π:
gh copilot suggest 'how to exit vim' -t shell
I was provided with the following suggestion:
:q!
Though this is correct, it's not quite what I was looking for, because while this will take you out of vim, you will loose all your changes in the process. So I asked copilot to revise the command:
? How should this be revised?
> how to exit vim and save changes
Suggestion:
:wq
Perfect! ππΌ
See this entire flow in the visual example below:
Conclusion
GitHub Copilot in the CLI is a game-changer that is super useful for reminding you of commands, teaching you new commands or explaining random commands you come across find online.
I hope you enjoy using it! Try it yourself and let me know how you used it. If you have feedback or issues, you can submit an issue in the gh-copilot repo or leave a comment below and I'll be sure to get you an answer.
Have you tried the GitHub Copilot in the CLI? What do you think? Let me know in the comments below! ππΌπ
Top comments (8)
Great wrote up. Had GH installed for a while but hadn't spotted this. I started building a related AI thing called starpilot a few weeks ago because I wanted a way to discover hidden gems in my GitHub stars, but maybe they're going to scoop me on it with tools like this!
oh that's soo cool! Starpilot looks promising Would love to have you on open source fridays if you wanna show it off!
I was inspired by what Simon said about blogging in this ep I watched this morning and spent some time this afternoon dusting off the old dev.to blog and wrote up a post on starpilot :)
Sure, what is OpenSource Fridays?
EDIT: Ha, just found what you mean. answer is still sure :)
To add gcp(or any term) as your github copilot alias on Windows command prompt, do the following:
doskey gcp=gh copilot $*
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor
into the address barAutoRun
and pasteC:\Windows\aliases.cmd
as the key's value.@ladykerr Maybe you can add this to the article, since Windows currently has 70% marketshare for Desktop users. I think it would be super helpful.
Update: Actually don't do that because even though it works you see the alias being set every time you run cmd. Instead use bgreco.net/alias/, and after installing it type
alias gcp=gh copilot
in command prompt.That's it.
That must be the most junior paragraph ever written.
This "article" is also literally about 2 subcommands.
How do you think is your "article" better than much more concise docs docs.github.com/en/copilot/github-...?
On the other hand I love the screebshots/gifs and that it is relatively "in-depth".
This blog is not intended to be better than the official docs. It is a guide on how to use copilot in the cli. Feel free to download the tool and ask it more advanced questions. This blog was written for devs across all experience levels in mind.