Skip to content

Instantly share code, notes, and snippets.

@Konijima
Last active April 18, 2025 18:04
Show Gist options
  • Save Konijima/01c11edd9c089da10b5a4df46e19c894 to your computer and use it in GitHub Desktop.
Save Konijima/01c11edd9c089da10b5a4df46e19c894 to your computer and use it in GitHub Desktop.
Copilot CLI - how & what
# Copilot CLI - how & what
# GIST: https://gist.github.com/Konijima/01c11edd9c089da10b5a4df46e19c894
# Load Copilot CLI aliases
eval "$(gh copilot alias -- bash)"
# Define the 'how' command
function how() {
local system="$(uname -srm)"
local shell="$(ps -p $$ -o comm=)"
# Append Termux context if detected
if [[ -n "$TERMUX_VERSION" || "$PREFIX" == "/data/data/com.termux/files/usr" ]]; then
system="Termux on $system"
fi
local system_info="[SHELL: $shell | SYSTEM: $system]"
# Build the full query with contextual info
local query="Context: $system_info | Prompt: $*"
# Execute Copilot CLI non-interactively and capture output
local output
if ! output=$(yes "" 2>/dev/null | ghcs "$query" 2>&1); then
echo "[how] Error: Copilot CLI failed to respond or crashed."
echo "[how] Try rephrasing your prompt and run the command again."
return 1
fi
# Extract and print only the suggested command
echo "$output" \
| awk '/Suggestion:/{flag=1; next} /^\? Select an option/{flag=0} flag {print}' \
| sed '/^$/d; s/[[:space:]]*$//'
tput cnorm
}
# Define the 'what' command
function what() {
local output="$(ghce "$@")"
# Extract and print only the explanation
echo "$output" | awk '/^ *# Explanation:/ {found=1; next} found'
}
@Konijima
Copy link
Author

Konijima commented Apr 18, 2025

πŸš€ how & what β€” GitHub Copilot CLI Shortcuts

(Non-Interactive Copilot Command Suggester & Explainer)

This script defines two helper functions, how and what, which use the GitHub Copilot CLI to either suggest shell commands (how) or explain shell commands (what) in a clean, non-interactive format.


πŸ“¦ Features

  • ✨ Uses gh-copilot
  • πŸ–₯️ Adds platform context (e.g., Linux, macOS, Termux) for smarter suggestions
  • 🧼 Removes UI prompts and menus β€” outputs are clean and scriptable
  • 🧠 Accepts unquoted input: how install nginx or what git rebase -i
  • πŸ“‹ Automatically copies suggestions to clipboard (handled by Copilot CLI)
  • πŸ”§ Lightweight, portable β€” no extra dependencies beyond GitHub CLI

πŸ“š Function Overview

πŸ› οΈ how "<natural language prompt>"

Generate a command from a prompt:

how install nginx

Returns something like:

sudo apt install nginx
  • Adds contextual system/shell info to improve suggestions
  • Captures only the command text (strips out prompts, menus, and noise)

πŸ“– what "<actual command>"

Explain a real command:

what git lfs migrate import --everything --include="*.gz,*.png"

Returns:

The command migrates all matching files in your Git history into Git LFS...
  • Direct wrapper for gh copilot explain
  • Non-interactive, outputs only the explanation

πŸ› οΈ Installation

1. Install GitHub CLI

macOS/Linux (Homebrew):

brew install gh

Debian/Ubuntu (APT):

sudo apt install gh

2. Install Copilot CLI Extension

gh extension install github/gh-copilot

3. Authenticate with GitHub

gh auth login

4. Add how and what Functions

Install both helper functions with a single script:

URL=https://gist.githubusercontent.com/Konijima/01c11edd9c089da10b5a4df46e19c894/raw/.bashrc && \
FILE=~/.bashrc-gh-copilot && curl -fsSL "$URL" -o "$FILE" && \
RC=$(basename "$SHELL") && case "$RC" in \
  bash) TARGET=~/.bashrc ;; \
  zsh)  TARGET=~/.zshrc  ;; \
  *) echo "Unsupported shell: $RC (only bash and zsh are supported)"; exit 1 ;; \
esac && grep -qxF "source $FILE" "$TARGET" || echo "source $FILE" >> "$TARGET" && \
source "$FILE"

βœ… Example Usage

how install ffmpeg
how "generate ssh key and add it to GitHub"
what git log --graph --oneline --all

🧠 System Context Awareness

The how function automatically appends:

  • Shell name (e.g., bash, zsh)
  • System version (e.g., Linux 6.8.9 x86_64)
  • Adds Termux context if applicable

This provides Copilot with smarter, platform-aware suggestions.


πŸ” Authentication Check

Ensure your GitHub CLI is connected and has Copilot access:

gh auth status

πŸ“‚ Source

Gist: https://gist.github.com/Konijima/01c11edd9c089da10b5a4df46e19c894

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment