Add aliases to GitHub CLI from stdin
With the GitHub CLI
before 1.10 as described originally in gh user
, adding aliases with multiple lines or mixed quotes could be difficult. After some time I found it easier and faster just to open ~/.config/gh/config.yml and write the YAML string literal manually. Starting with gh version 1.10, my PR
makes that even easier in bash using a herestring as an example:
gh alias set -s hello - << 'EOF'
echo "Hello, \e[32m$USER\e[0m!"
EOF
Note: putting EOF in quotes disables variable expansion, command substitution, and more.
You can use herestrings in PowerShell as well, shown here using a literal herestring to also avoid variable expansion:
@'
echo "Hello, \e[32m$USER\e[0m!"
'@ | gh alias set -s hello -
In both cases, shell commands are executed in sh that is installed with git.
You can just as easily use files for input. If you have a file named command.txt with a complicated GraphQL expression, for example, you could import it into an alias named user like so:
cat command.txt | gh alias set user -
gh aliases can be very useful - especially for gh api - and I hope this makes them a little easier to set.