Skip to content

Instantly share code, notes, and snippets.

@gsusI
Created April 30, 2023 08:27
Show Gist options
  • Save gsusI/1df1fcd4bef2c9e5956727e611894539 to your computer and use it in GitHub Desktop.
Save gsusI/1df1fcd4bef2c9e5956727e611894539 to your computer and use it in GitHub Desktop.
Bash script to save prompt and resulting command to history using GitHub Copilot CLI
copilot_what-the-shell () {
history -s "?? $@"; # Save the prompt
TMPFILE=$(mktemp);
trap 'rm -f $TMPFILE' EXIT;
if /usr/bin/github-copilot-cli what-the-shell "$@" --shellout $TMPFILE; then
if [ -e "$TMPFILE" ]; then
FIXED_CMD=$(cat $TMPFILE);
history -s "$FIXED_CMD";
eval "$FIXED_CMD"
else
echo "Apologies! Extracting command failed"
fi
else
return 1
fi
};
alias '??'='copilot_what-the-shell';
copilot_git-assist () {
history -s "git? $@"; # Save the prompt
TMPFILE=$(mktemp);
trap 'rm -f $TMPFILE' EXIT;
if /usr/bin/github-copilot-cli git-assist "$@" --shellout $TMPFILE; then
if [ -e "$TMPFILE" ]; then
FIXED_CMD=$(cat $TMPFILE);
history -s "$FIXED_CMD";
eval "$FIXED_CMD"
else
echo "Apologies! Extracting command failed"
fi
else
return 1
fi
};
alias 'git?'='copilot_git-assist';
copilot_gh-assist () {
history -s "gh? $@"; # Save the prompt
TMPFILE=$(mktemp);
trap 'rm -f $TMPFILE' EXIT;
if /usr/bin/github-copilot-cli gh-assist "$@" --shellout $TMPFILE; then
if [ -e "$TMPFILE" ]; then
FIXED_CMD=$(cat $TMPFILE);
history -s "$FIXED_CMD";
eval "$FIXED_CMD"
else
echo "Apologies! Extracting command failed"
fi
else
return 1
fi
};
alias 'gh?'='copilot_gh-assist';
alias 'wts'='copilot_what-the-shell';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment