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! 👇🏼🚀