[英]How do I make my zsh prompt look like sh/bash end with $ instead of %?
Can anyone show how to modify the following zsh prompt environment var assignment to make its emitted string end with $
(followed by a space), instead of %
?任何人都可以展示如何修改以下 zsh 提示环境变量赋值,使其发出的字符串以$
(后跟一个空格)结尾,而不是%
?
export PROMPT='%F{111}%m:%F{2}%~ %#%f '
macOS Catalina changed the default shell to zsh, and I saw an article that encouraged a switch. macOS Catalina 将默认 shell 改为 zsh,看到一篇文章鼓励切换。 I assumed backward compatibility but prompt logic changed.我假设向后兼容,但提示逻辑改变了。
export PROMPT='%F{111}%m:%F{2}%~ $%f '
有关 zsh 提示字符串自定义的更多选项 - http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Login-information
%(!.#.$)
简答:使用%(!.#.$)
The portion of your prompt string that's generating the %
is %#
-- which generates #
if the shell is a privileged (root) shell, and %
otherwise.生成%
提示字符串部分是%#
- 如果 shell 是特权(root)shell,则生成#
否则生成%
。
If you still want that general class of behavior, just with $
instead of %
, you can do %(!.#.$)
in place of %#
.如果您仍然想要那种一般行为,只需使用$
而不是%
,您可以使用%(!.#.$)
代替%#
。 This is a conditional, with three parts (each separated by .
): !
这是一个条件,包含三个部分(每个部分以.
分隔): !
checks to see if the shell is privileged, the #
is the expansion value if the check comes back true, and the $
is the expansion value if it's false.检查 shell 是否有特权,如果检查结果为真, #
是扩展值,如果为假,则$
是扩展值。
A demonstration session (in a shell with a starting prompt of simply %
( PROMPT='%% '
)):演示会话(在 shell 中,启动提示仅为%
( PROMPT='%% '
)):
% PROMPT=': %#; ' ;: # the default %# (unwanted %, just to show it)
: %; sudo -s ;: # prompt is % now -- until we switch to root
: #; exit ;: # prompt is # now -- exit to return to user
: %; PROMPT=': %(!.#.$); ' ;: # set new prompt string with conditional for #/$
: $; sudo -s ;: # prompt is $ now -- what we wanted
: #; exit ;: # prompt is # for root shells, still
: $; ;: # back to user shell, prompt back to $
Folding this in to your prior settings, we have:将其折叠到您之前的设置中,我们有:
export PROMPT='%F{111}%m:%F{2}%~ %(!.#.$)%f '
Should do the trick!应该做的伎俩!
(As a random side-note, I'm using ;
and the builtin :
command to effect comments where they might not otherwise be allowed -- and in particular, to treat the prompt itself as a comment, so it can be copy/pasted along with a command to re-run the command -- at the expense of some noise in your shell where you paste it. Also, the above example assumes sudo has been run recently enough, and/or is otherwise configured, to not require a password -- of course, if it did, this would all still work, it would just be extra noise in the example.) (作为一个随机的旁注,我正在使用;
和内置的:
命令来影响可能不允许的注释——特别是,将提示本身视为注释,因此可以复制/粘贴以及重新运行命令的命令——代价是你粘贴它的 shell 中的一些噪音。此外,上面的例子假设 sudo 最近已经运行足够多,和/或以其他方式配置,不需要密码——当然,如果是这样,这一切仍然有效,只是在示例中会产生额外的噪音。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.