Better AI in CLI with Github Copilot!

Feb 11, 2025

Until now, I knew only one tool that I could run a CLI to work with AI - as only Github Copilot has been approved as a daily AI provider - an official gh copilot extension. But there is another!

GH Copilot

⚠️
This post is not about gh copilot extension but we have to start with that so we have comparison to another solution.

Install

  1. Install gh
  2. Install gh copilot with: gh extension install github/gh-copilot

Possibilites

.. are limited.

❯ gh copilot -h                         
Your AI command line copilot.

Usage:
  copilot [command]

Examples:

$ gh copilot suggest "Install git"
$ gh copilot explain "traceroute github.com"


Available Commands:
  alias       Generate shell-specific aliases for convenience
  config      Configure options
  explain     Explain a command
  suggest     Suggest a command

Flags:
  -h, --help              help for copilot
      --hostname string   The GitHub host to use for authentication
  -v, --version           version for copilot

Use "copilot [command] --help" for more information about a command.

Basically, only those two commands can be run.


It's funny; if I'm using a free Copilot version and added to a GitHub organization that doesn't have Copilot enabled, I cannot use it.

❯ gh copilot explain

✗ Error: User is not authorized to use this Copilot feature

To start using GitHub Copilot in the CLI, request access from your organization admin.
If you have access to GitHub Copilot via your organization or enterprise, you cannot use Copilot in the CLI if your organization owner or enterprise administrator has disabled Copilot in the CLI.

~ https://docs.github.com/en/copilot/managing-copilot/configure-personal-settings/installing-github-copilot-in-the-cli#prerequisitesWhat's funny is that it seems that if I'm using a free Copilot version and I'm added to GitHub Organisation that doesn't have Copilot enabled, I cannot use that.


Here is how it works if it works:

❯ gh copilot explain -h
Explain a given input command in natural language.

Did you ever wonder what a command does? Or why it was suggested to you? Now you can find out!

Usage:
  copilot explain [command] [flags]

Examples:

# View disk usage, sorted by size
$ gh copilot explain 'du -sh | sort -h'

# View git repository history as text graphical representation
$ gh copilot explain 'git log --oneline --graph --decorate --all'

# Remove binary objects larger than 50 megabytes from git history
$ gh copilot explain 'bfg --strip-blobs-bigger-than 50M'


Flags:
  -h, --help   help for explain

Global Flags:
      --hostname string   The GitHub host to use for authentication

gh copilot explain

❯ gh copilot explain

Welcome to GitHub Copilot in the CLI!
version 1.0.6 (2025-01-06)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency

? Which command would you like to explain? 
> ls -al

Explanation:                                                                                                                                   
                                                                                                                                               
  • ls is used to list files and directories.                                                                                                  
    • -a lists all files, including hidden files.                                                                                              
    • -l provides a long listing format.  

gh copilot explain

❯ gh copilot suggest -h
Suggest a command based on a natural language description of the desired output effect.

There is no task too small or too large to need help when working in the terminal, the only limit is your imagination:

- installing and upgrading software
- troubleshooting and debugging problems with your system
- processing and manipulating files
- working with git and GitHub in the terminal

Usage:
  copilot suggest [prompt] [flags]

Examples:

- Guided experience
  $ gh copilot suggest

- Git use cases
  $ gh copilot suggest -t git "Undo the most recent local commits" 
  $ gh copilot suggest -t git "Clean up local branches" 
  $ gh copilot suggest -t git "Setup LFS for images" 

- Working with the GitHub CLI in the terminal
  $ gh copilot suggest -t gh "Create pull request"
  $ gh copilot suggest -t gh "List pull requests waiting for my review"
  $ gh copilot suggest -t gh "Summarize work I have done in issues and pull requests for promotion"

- General use cases
  $ gh copilot suggest -t shell "Kill processes holding onto deleted files"
  $ gh copilot suggest -t shell "Test whether there are SSL/TLS issues with github.com"
  $ gh copilot suggest -t shell "Convert SVG to PNG and resize"
  $ gh copilot suggest -t shell "Convert MOV to animated PNG"


Flags:
  -h, --help               help for suggest
  -s, --shell-out string   Path to file for outputting command to execute
  -t, --target target      Target for suggestion; must be shell, gh, git

Global Flags:
      --hostname string   The GitHub host to use for authentication

gh copilot suggest

❯ gh copilot suggest -t shell "list directories"

Welcome to GitHub Copilot in the CLI!
version 1.0.6 (2025-01-06)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency

Suggestion:                                                                                                                                    
                                                                                                                                               
  ls -d */                                                                                                                                     

? Select an option  [Use arrows to move, type to filter]
> Copy command to clipboard
  Explain command
  Execute command
  Revise command
  Rate response
  Exit

gh copilot suggest

Mods

GitHub - charmbracelet/mods: AI on the command line
AI on the command line. Contribute to charmbracelet/mods development by creating an account on GitHub.

So this new alternative is to use mods from Charm_ company, recently started supporting Github Copilot as an LLM provider.

Install

version: 3

tasks:
  install:mods:
    desc: |
      Mods is AI CLI app from CharmBracelet
    cmds:
      - brew install charmbracelet/tap/mods
    status:
      - command -v mods

Taskfile.yaml

Configure

Run mods --settings and:

  1. Remove all LLM providers that you don't want to use from apis section.
    1. On Linux, a configuration is saved to ~/.config/mods/mods.yml (can be checked with mods --dirs )
    2. I recommend adding that file to any dotfile manager (like chezmoi )

Configure GitHub Copilot

💡
You can use it with Github Copilot free tier!
  1. Leave only apis.copilot as LLM providers in the settings file.
  2. Download and install Zed code editor:
    1. curl -f https://zed.dev/install.sh | sh
  3. Log in to Github Copilot (button in the bottom right corner)
  1. Test it out:
❯ mods Hello     

  Hello! How can I assist you today?                                              

Conversation saved:  be2972e  Hello

Testing

Questions for files

❯ cat README.md | mods "What profiles are available?"

  The profiles available are:                                                     
                                                                                  
  •  ubuntu                                                                       
  •  mirai                                                                                                        

Conversation saved:  1dddc87  What profiles are available?

Returning commands

  1. Configure role (from example in docs)
roles:
  shell:
    - you are a shell expert
    - you do not explain anything
    - you simply output one liners to solve the problems you're asked
    - you do not provide any explanation whatsoever, ONLY the command
  1. Run command
❯ mods -R shell "I need a command to list all files bigger than 5 M"

                                                                                  
    find . -type f -size +5M                                                      


Conversation saved:  8d20ad2  I need a command to list all files bigger than 5 M

List conversations (interactive)

❯ mods -l                        
┃ Conversations                                                                                                                                                    
┃ > 8d20ad2  I need a command to list all files bigger than 5 M 30 seconds ago gpt-4o                                                                              
┃   695a37f  shell I need a command to list all files bigger than 5 M 35 seconds ago gpt-4o                                                                        
┃   5afc8f4  shell I need a command to list all files bigger than 5 M 1 minute ago gpt-4o                                                                          
┃   1dddc87  What profiles are available? 3 minutes ago gpt-4o                                                                                                     
┃   be2972e  Hello 5 minutes ago gpt-4o                                                                                                                            
                                                                                                                                                                   
↑ up • ↓ down • / filter • enter submit

Quick Chat

(just type mods )

☸ backbone in lib-system-manager on  main 
❯ mods       

  OpenTelemetry is an open-source observability framework for cloud-native        
  software                                                                        
  that provides APIs, libraries, agents, and instrumentation to help developers   
  generate, collect, and export telemetry data, including metrics, logs, and      
  traces, for monitoring and performance analysis.                                

Conversation saved:  a2dde37  What de heck is OpenTelemetry? Be brief

Reviewer

❯ mods -f "what are your thoughts on improving this code?" < main.go | glow

Conversation saved:  3a0fe05  what are your thoughts on improving this code?

  Your Go code looks fairly good but there are always ways to enhance readability,
  maintainability, and performance when necessary. Below are some overall         
  improvements and explanations:                                                  
                                                                                  
  1. Consistent Error Handling: Use the  log.Fatalf  directly or have error       
  handling with proper messages and exit codes for better clarity and debugging.  
  2. Token Source Initialization: Ensure that the OAuth2 token initialization is  
  properly uncommented and put to use.                                            
  3. Separated Concerns: Separate setup and logic into different functions to     
  improve readability and maintainability.                                        
  4. Constants: Use constants where applicable for values like environment        
  variable keys.                                                                  
  5. Logging: Add more descriptive logging messages.                              
  6. Using  context.WithTimeout : It can be beneficial to add timeouts for network
  operations to avoid hanging processes.                                          
                                                                                  
  Here's the improved version of your code:                                       
                                                                                  
  < REDACTED >                                                                            
                                                                                  
  ### Key Changes:                                                                
                                                                                  
  1. Separated Logic: The code is now divided into helper functions which makes   
  main()  cleaner and more readable.                                              
  2. Timeout Context: Using  context.WithTimeout  to prevent the program from     
  potentially hanging indefinitely if API calls take too long.                    
  3. Environment Variable Fetching: Centralized fetching and validation of        
  environment variables in the  getEnv  function.                                 
  4. Error Handling: Added consistent and descriptive error handling across the   
  codebase.                                                                       
  5. Filtered Comments Processing: Filtered comments by user before processing and
  deleting, thus ensuring we only loop through relevant comments.                 
  6. Logs: Enhanced logging for better debugging and understanding runtime        
  behavior. 

Krzysztof Wiatrzyk

Big love for Kubernetes and the entire Cloud Native Computing Foundation. DevOps, biker, hiker, dog lover, guitar player, and lazy gamer.