简体   繁体   English

如何让我的 zsh 提示看起来像 sh/bash 以 $ 而不是 % 结尾?

[英]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

Short answer: use %(!.#.$)简答:使用%(!.#.$)


Long answer:长答案:

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.

相关问题 如何根据条件格式化zsh提示符? - How do I format my zsh prompt based on a condition? 如何让 zsh prompt vi 模式使用我的 .vimrc? - How to make zsh prompt vi mode use my .vimrc? 如何在ZSH提示中输出当前历史记录事件编号(%!或%h)-1? - How do I output the current history event number (%! or %h) -1 in my ZSH prompt? 如何从 .bash_profile 访问我的 nvm 节点版本号以自定义我的终端提示? - How do I access my nvm node version number from .bash_profile to customize my terminal prompt? 如何使用zsh永久更改iTerm2中的终端提示? - How do I permanently change the terminal prompt in iTerm2 using zsh? 如何为终端制作动画bash shell提示符? - How to make an animated bash shell prompt for the terminal? 如何为命令提示符创建自己的外部命令? - How do I make my own external command for a command-prompt? Bash 和 Zsh 提示响铃并显示最后一条命令的错误代码 - Bash and Zsh prompt that ring a bell and do display error code of last command 如何使终端提示延长终端的宽度? - How can I make my terminal prompt extend the width of the terminal? 如何将 bash 变量中的字体颜色和样式传递给 PS1 提示符 - How do I pass a font color and style from bash variable to the PS1 prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM