GPG Agent
What
Display whether a gpg-agent is running and how git signs your commits. With fetch_keys enabled, also
show which of its keys are unlocked, so you spot a locked key before git commit asks for a passphrase, and give your
keys friendly names with mapped_keys.
Sample Configuration
- json
- yaml
- toml
Options
| Name | Type | Default | Description |
|---|---|---|---|
cache_duration | string | 10s | how long the agent and git answers are cached |
fetch_keys | boolean | false | ask the agent about its keys, see below |
key_template | string | template for each key | |
key_separator | string | " " | text between keys |
mapped_keys | map[string]string | keygrip-to-alias map |
By default key_template is {{ if .Cached }}{{ or .Alias .Keygrip }}{{ end }}, so only unlocked keys are listed.
The agent is queried with gpg-connect-agent --no-autostart, so the segment never starts an agent by itself. Both the
agent answer and the git configuration are cached for cache_duration to keep the prompt fast.
Without fetch_keys, the segment only asks the agent whether it is alive with GETINFO pid, which leaves the
passphrase cache untouched. With fetch_keys, it runs keyinfo --list, and every read of a cached passphrase resets
that passphrase's idle timer in gpg-agent. Because the prompt is redrawn constantly, an unlocked key then stays
unlocked far beyond default-cache-ttl, for as long as you keep using the shell. Enable it only if you accept that
trade-off; a longer cache_duration reduces how often it happens but does not remove it.
Use the keygrip as the map key, as listed by gpg --list-secret-keys --with-keygrip. Its value is available as .Alias
in key_template; see Key Properties for the available properties.
Template (info)
- json
- yaml
- toml
{
"template": "{{ if .Running }} {{ if .Count }}{{ .CachedCount }} {{ end }}{{ end }}"
}
template: "{{ if .Running }} {{ if .Count }}{{ .CachedCount }} {{ end }}{{ end }}"
template = "{{ if .Running }} {{ if .Count }}{{ .CachedCount }} {{ end }}{{ end }}"
Properties
| Name | Type | Description |
|---|---|---|
.Running | boolean | whether a gpg-agent answered |
.Count | int | the number of keys known to the agent, 0 without fetch_keys |
.CachedCount | int | the number of unlocked keys, 0 without fetch_keys |
.Keys | string | the keys rendered with key_template, empty without fetch_keys |
.SigningKey | string | the git user.signingkey of the current directory |
.SignCommits | boolean | whether git commit.gpgsign is enabled |
.TTYSet | boolean | whether GPG_TTY is set; without it pinentry often fails to prompt |
.SSHSupport | boolean | whether SSH_AUTH_SOCK points to the gpg-agent SSH socket (enable-ssh-support) |
.Count, .CachedCount, .Keys and the key properties below are only filled in when fetch_keys is enabled, so
the default template shows the count only once you opt in. Keys for which key_template renders nothing are left out
of .Keys, but still count towards .Count.
.SSHSupport checks that SSH_AUTH_SOCK ends with the default socket name S.gpg-agent.ssh. A gpg-agent SSH socket
under a custom name, for example one set with --extra-socket, is not detected.
.SigningKey and .SignCommits are read with git config for the current directory and cached for cache_duration.
Key Properties
Available in key_template:
| Name | Type | Description |
|---|---|---|
.Alias | string | the mapped_keys value for this key's keygrip, if any |
.Cached | boolean | whether the passphrase of this key is cached |
.Keygrip | string | the 40 character keygrip identifying the key in the agent |
.TTL | int | seconds until the cached passphrase expires, 0 when it is not cached |
Older gpg-agent releases report fewer fields per key: such a key is still counted in .Count, with .TTL at 0,
and .Cached false when the agent does not report the cache state at all.
Examples
Warn when a commit would need a passphrase, with fetch_keys enabled:
{{ if and .SignCommits (eq .CachedCount 0) }} {{ "" }} locked {{ end }}
Warn about a missing GPG_TTY:
{{ if not .TTYSet }} GPG_TTY unset {{ end }}
List every key and its state, with fetch_keys enabled and key_template set to
{{ or .Alias .Keygrip }}{{ if .Cached }} ({{ .TTL }}s){{ else }} (locked){{ end }}:
{{ if .Running }} {{ "" }} {{ .Keys }} {{ end }}