Sub topic 13
Aliases and configuration
This chapter assumes you can run gh commands for pull requests and issues, and that you are logged in. It covers two ways to tailor gh: aliases for shortcuts, and configuration for default behavior.
Aliases
An alias is a short name that expands to a longer gh command. You define one with gh alias set, and it works like a built-in command.
gh alias set mine 'pr list --author @me'
gh mineRunning gh mine is now the same as running gh pr list --author @me. Any extra arguments you pass are appended to the expanded command, so gh mine --state merged also works.
Arguments and placeholders
Use $1, $2, and so on when an argument must appear in the middle of the expansion.
gh alias set bugs 'issue list --label bug --assignee $1'
gh bugs octocatIf an alias uses a placeholder and you leave the argument out, gh reports an error instead of running a partial command.
Shell aliases
A normal alias can only expand to other gh commands. To use pipes, grep, or other programs, create a shell alias with --shell. The expansion then runs through sh.
gh alias set --shell recent 'gh pr list --json number,title --jq ".[] | .title" | head -n 5'
gh recentShell aliases receive arguments as $1, $2, and so on, and also as "$@".
Managing aliases
gh alias list
gh alias delete minegh alias set refuses to replace a built-in command such as pr. It will overwrite an existing alias only if you pass --clobber.
Configuration
gh keeps settings in a config.yml file, normally under ~/.config/gh/. You rarely edit it by hand. Use gh config instead.
gh config list
gh config get git_protocol
gh config set git_protocol ssh
gh config set editor "code --wait"
gh config set prompt disabledCommon keys:
| Key | Effect |
|---|---|
| `git_protocol` | Use `https` or `ssh` when cloning and adding remotes |
| `editor` | Editor opened for issue and pull request text |
| `prompt` | Set to `disabled` to turn off interactive prompts |
| `pager` | Program used to page long output |
| `browser` | Program used by `--web` flags |
Your aliases are stored in the same file, under an aliases key.
Environment variables override the file
Environment variables take precedence over config.yml. For example, GH_EDITOR, GH_PAGER, GH_BROWSER, and GH_PROMPT_DISABLED each override the matching setting for a single command or session. GH_CONFIG_DIR points gh at a different configuration directory.
GH_EDITOR=nano gh issue create
GH_CONFIG_DIR=/tmp/gh-test gh config listUse the file for your permanent preferences and environment variables for scripts or one-off changes.
Common errors
- An alias is rejected as a built-in name. Pick a name that is not an existing command, such as
mineinstead ofpr. - A pipe inside a plain alias fails. Plain aliases do not run through a shell. Recreate it with
--shell. - Quotes disappear. Wrap the whole expansion in single quotes, and use double quotes inside it for
jqfilters. - A setting seems ignored. Check whether a
GH_*environment variable is set, since it wins over the file.
Exercise
You want gh pending to list your open pull requests that are still drafts, using gh pr list --author @me --draft. Which command defines the alias, and why does it not need --shell?
Show answer
gh alias set pending 'pr list --author @me --draft'. The expansion consists only of a gh command, so a normal alias is enough. --shell is needed only when the expansion uses pipes or non-gh programs.
Exercise
Your config sets editor to vim, but you want nano for one issue only. How do you do this without changing the config file?
Show answer
Run GH_EDITOR=nano gh issue create. The environment variable overrides the editor value in config.yml for that command.