Skip to content

Instantly share code, notes, and snippets.

@ardnew
Last active April 14, 2024 20:09
Show Gist options
  • Save ardnew/497fc3ed4864fb392d4cf38249d2f1ab to your computer and use it in GitHub Desktop.
Save ardnew/497fc3ed4864fb392d4cf38249d2f1ab to your computer and use it in GitHub Desktop.
bash functions to abbreviate github/gh-copilot commands
#!/opt/bash/bin/bash
if ghbin=$( type -P gh ); then
copilot="github/gh-copilot"
if "${ghbin}" extension list 2>&1 | cut -s -f2 | grep -q "^${copilot}$"; then
function ? {
[[ ${#} -gt 0 ]] || set -- --help
case ${1} in
git) set -- --target git "${@:2}" ;;
gh|github) set -- --target gh "${@:2}" ;;
--*) ;;
sh|shell|bash) set -- "${@:2}" ;&
*) set -- --target shell "${@}" ;;
esac
"${ghbin}" copilot suggest "${@}"
}
function ?? { "${ghbin}" copilot explain "${@}" ; }
fi
fi
@ardnew
Copy link
Author

ardnew commented Dec 12, 2023

Important

The command ? is effectively an alias for gh copilot suggest, and the command ?? is effectively an alias for gh copilot explain.

Given no other arguments, each will print their respective --help flag.

For ? (i.e., suggest), if the first word is a recognized target, or a similar, related word (e.g., sh and bash will both use target shell, and github will use target gh), then the target flag is inserted with recognized argument prior the remaining command line arguments.

If the first word is not a recognized target, then --target shell is inserted automatically.

For ?? (i.e., explain), all arguments are forwarded through verbatim.

Tip

Examples:

?                      # => gh copilot suggest --help
? sh foo               # => gh copilot suggest --target shell foo
? foo                  # => gh copilot suggest --target shell foo
??                     # => gh copilot explain --help
?? -x foo              # => gh copilot explain -x foo

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