Doc: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html

doesn't seem to work in this use case.

Use case: if prompt contains localhost then remove hostname from prompt, if not prompt contains hostname like:

(check %m contains 'localhost' then result empty, if not the use %m in prompt)


%m expands to the value of $HOST (initialised at startup from the host name).

So you could use:

set -o promptsubst

Or, as $HOST would typically not change (not even when the host name changes), and assuming it doesn't contain % characters:


(expand it in your ~/.zshrc).

  • I need it in %m instead of expand t $HOST, I use PROMPT= in zshrc, and somehow the expaned doesn't effect. – Tuyen Pham Nov 1 '18 at 17:38
  • Even though, I end up with some if/then to set PROMPT. Thanks. – Tuyen Pham Nov 1 '18 at 18:22
  • @TuyenPham PS1 is the same as PROMPT (one is from Bourne sh, one from csh,). %m is the same as $HOST. For expansions to be done in the prompts, you need the promptsubst option. – Stéphane Chazelas Nov 1 '18 at 19:20
  • Thanks, I tested your setup again using print -P '${HOST##*localhost*}'and I've noticed that If I remove one or more of ocalhos, reuslt is print out localhost and is expected. But if I remove l of lo or t of st, result print out empty - means same result as whole word localhost. How it can be fixed? Why I can't tag you? – Tuyen Pham Nov 4 '18 at 6:28
  • @TuyenPham, I don't understand what you mean ${HOST##*localhost*} is meant to expand to the empty string if $HOST contains localhost and to $HOST otherwise as requested. There's no "tag" completion offered for me as I'm the author of the article you're commenting on so I'm always notified anyway. – Stéphane Chazelas Nov 4 '18 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.