Last active
April 18, 2025 18:04
-
-
Save Konijima/01c11edd9c089da10b5a4df46e19c894 to your computer and use it in GitHub Desktop.
Copilot CLI - how & what
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
π
how & what
β GitHub Copilot CLI Shortcuts(Non-Interactive Copilot Command Suggester & Explainer)
This script defines two helper functions,
how
andwhat
, which use the GitHub Copilot CLI to either suggest shell commands (how
) or explain shell commands (what
) in a clean, non-interactive format.π¦ Features
Linux
,macOS
,Termux
) for smarter suggestionshow install nginx
orwhat git rebase -i
π Function Overview
π οΈ
how "<natural language prompt>"
Generate a command from a prompt:
π
what "<actual command>"
Explain a real command:
what git lfs migrate import --everything --include="*.gz,*.png"
gh copilot explain
π οΈ Installation
1. Install GitHub CLI
macOS/Linux (Homebrew):
Debian/Ubuntu (APT):
2. Install Copilot CLI Extension
3. Authenticate with GitHub
4. Add
how
andwhat
FunctionsInstall both helper functions with a single script:
β 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:bash
,zsh
)Linux 6.8.9 x86_64
)Termux
context if applicableThis provides Copilot with smarter, platform-aware suggestions.
π Authentication Check
Ensure your GitHub CLI is connected and has Copilot access:
π Source
Gist: https://gist.github.com/Konijima/01c11edd9c089da10b5a4df46e19c894