From 0fa5bdc030abc12547bfabc3ce536f482a51479d Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Wed, 13 May 2026 15:33:52 +0000 Subject: [PATCH] Add option to disable hostname in prompt Fixes #614 --- pure.zsh | 24 +++++++++++++++++++----- readme.md | 4 ++++ tests/preview.zsh | 13 +++++++++++++ 3 file(s) changed, 36 insertion(s)(+), 5 deletion(s)(-) diff --git a/pure.zsh b/pure.zsh --- a/pure.zsh +++ b/pure.zsh @@ -67,9 +67,9 @@ # Don't set title over serial console. /dev/ttyS[0-9]*) return;; esac - # Show hostname if connected via SSH. + # Show hostname if connected via SSH and host display is enabled. local hostname= - if (( psvar[13] )); then + if (( psvar[13] )) && (( ${prompt_pure_state[show_host]:-1} )); then # Expand in-place in case ignore-escape is used. hostname="${(%):-(%m) }" fi @@ -817,10 +817,15 @@ # Set psvar[13] flag for username display in PROMPT. [[ -n $user_color ]] && psvar[13]=1 + # Check if hostname display is enabled (default: yes). + local show_host=1 + zstyle -T ":prompt:pure:host" show || show_host=0 + typeset -gA prompt_pure_state prompt_pure_state[version]="1.27.1" prompt_pure_state+=( user_color "$user_color" + show_host "$show_host" prompt "${PURE_PROMPT_SYMBOL:-❯}" ) } @@ -908,17 +913,22 @@ local path_sample="%F{$c[path]}~/dev/pure%f" if zstyle -t ':prompt:pure:path:separator' dim; then path_sample=$(prompt_pure_render_dimmed_path '~/dev/pure') + fi + + local host_sample='' + if zstyle -T ":prompt:pure:host" show; then + host_sample="%F{$c[host]}@heartofgold%f" fi # Sample preprompt with all components visible. - print -P "%F{$c[suspended_jobs]}${PURE_SUSPENDED_JOBS_SYMBOL-✦}%f %F{$c[user]}zaphod%f%F{$c[host]}@heartofgold%f ${path_sample} %F{$c[git:branch]}main%f%F{$c[git:dirty]}*%f %F{$c[git:action]}rebase-i%f %F{$c[git:arrow]}${PURE_GIT_DOWN_ARROW:-⇣}${PURE_GIT_UP_ARROW:-⇡}%f %F{$c[git:stash]}${PURE_GIT_STASH_SYMBOL-≡}%f %F{$c[node_version]}${node_symbol}22%f %F{$c[execution_time]}42s%f" + print -P "%F{$c[suspended_jobs]}${PURE_SUSPENDED_JOBS_SYMBOL-✦}%f %F{$c[user]}zaphod%f${host_sample} ${path_sample} %F{$c[git:branch]}main%f%F{$c[git:dirty]}*%f %F{$c[git:action]}rebase-i%f %F{$c[git:arrow]}${PURE_GIT_DOWN_ARROW:-⇣}${PURE_GIT_UP_ARROW:-⇡}%f %F{$c[git:stash]}${PURE_GIT_STASH_SYMBOL-≡}%f %F{$c[node_version]}${node_symbol}22%f %F{$c[execution_time]}42s%f" print -P "%F{$c[virtualenv]}venv%f %F{$c[prompt:success]}${PURE_PROMPT_SYMBOL:-❯}%f" print print -P "%F{$c[prompt:error]}${PURE_PROMPT_SYMBOL:-❯}%f prompt after error" print; print print -P "%F{$c[git:branch:cached]}main%f branch color when data is cached" print; print - print -P "%F{$c[user:root]}root%f%F{$c[host]}@heartofgold%f root user" + print -P "%F{$c[user:root]}root%f${host_sample} root user" print; print print -P "%F{$c[prompt:continuation]}… if%f %F{$c[prompt:success]}${PURE_PROMPT_SYMBOL:-❯}%f continuation prompt" } @@ -1015,7 +1025,11 @@ # myenv ❯ # # Preprompt line: each %(NV..) section only renders when its psvar is non-empty. PROMPT='%(12V.%F{$prompt_pure_colors[suspended_jobs]}%12v%f .)' - PROMPT+='%(13V.%F{$prompt_pure_colors['"${prompt_pure_state[user_color]:-user}"']}%n%f%F{$prompt_pure_colors[host]}@%m%f .)' + local hostname_part='' + if (( prompt_pure_state[show_host] )); then + hostname_part='%F{$prompt_pure_colors[host]}@%m%f' + fi + PROMPT+='%(13V.%F{$prompt_pure_colors['"${prompt_pure_state[user_color]:-user}"']}%n%f'"${hostname_part}"' .)' prompt_pure_set_path_separator PROMPT+='${${prompt_pure_path_separator_dimmed:+$(prompt_pure_render_dimmed_path)}:-${prompt_pure_path_segment}}' PROMPT+='%(14V. %F{${prompt_pure_git_branch_color}}%14v%(15V.%F{$prompt_pure_colors[git:dirty]}%15v.)%f.)' diff --git a/readme.md b/readme.md --- a/readme.md +++ b/readme.md @@ -125,6 +125,10 @@ Path separator dimming makes `/` characters in the path visually dimmer to help distinguish path components. It is not enabled by default. You can enable it with: `zstyle :prompt:pure:path:separator dim yes` +Hostname display is enabled by default when in an SSH session or container. You can disable it while still showing the username with: + +`zstyle :prompt:pure:host show no` + Automatic terminal title management can be disabled if you want to set your own tab or window titles: `zstyle :prompt:pure:title show no` diff --git a/tests/preview.zsh b/tests/preview.zsh --- a/tests/preview.zsh +++ b/tests/preview.zsh @@ -60,6 +60,19 @@ print -u2 "Preview did not apply dimmed path separators." return 1 fi + zstyle ':prompt:pure:host' show no + output=$(prompt_pure_preview 2>&1) + + if [[ $output == *'heartofgold'* ]]; then + print -u2 "Preview should not show hostname when host display is disabled." + return 1 + fi + + if [[ $output != *'zaphod'* ]]; then + print -u2 "Preview should still show username when host display is disabled." + return 1 + fi + print "preview tests passed." } -- tangled.sh