Skip to main content

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​

− ▢ ×  work _ ohmyposh.dev − ▢ ×  work _ ohmyposh.dev

Options​

NameTypeDefaultDescription
cache_durationstring10show long the agent and git answers are cached
fetch_keysbooleanfalseask the agent about its keys, see below
key_templatestringtemplate for each key
key_separatorstring" "text between keys
mapped_keysmap[string]stringkeygrip-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.

fetch_keys changes when your keys lock again

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)​

default template
{
"template": "{{ if .Running }}  {{ if .Count }}{{ .CachedCount }} {{ end }}{{ end }}"
}

Properties​

NameTypeDescription
.Runningbooleanwhether a gpg-agent answered
.Countintthe number of keys known to the agent, 0 without fetch_keys
.CachedCountintthe number of unlocked keys, 0 without fetch_keys
.Keysstringthe keys rendered with key_template, empty without fetch_keys
.SigningKeystringthe git user.signingkey of the current directory
.SignCommitsbooleanwhether git commit.gpgsign is enabled
.TTYSetbooleanwhether GPG_TTY is set; without it pinentry often fails to prompt
.SSHSupportbooleanwhether 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:

NameTypeDescription
.Aliasstringthe mapped_keys value for this key's keygrip, if any
.Cachedbooleanwhether the passphrase of this key is cached
.Keygripstringthe 40 character keygrip identifying the key in the agent
.TTLintseconds 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 }}