显示前一个命令退出代码时自定义 zsh 的提示 [英] Customize zsh's prompt when displaying previous command exit code

查看:21
本文介绍了显示前一个命令退出代码时自定义 zsh 的提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Zsh 包括使用 %? 转义序列在提示中显示前一个命令的返回代码/退出代码的功能.

Zsh includes the ability to display the return code/exit code of the previous command in the prompt by using the %? escape sequence.

但是我想有以下提示:

user@host ~ [%?] %

当退出代码与 0 不同时:

when the exit code is different from 0 and:

user@host ~ %

当退出代码为 0 时.

when exit code is 0.

如果我单独使用 %? 它总是显示,即使 %? 是 0.另外我想要方括号但只有当退出代码不是 0 时.

If I use %? alone it is always displayed, even if %? is 0. In addition I want the square brackets but only when the exit code not 0.

最简单的方法是什么?

推荐答案

PS1 中希望退出代码出现的位置添加:

Add this in the position in PS1 where you want the exit code to appear:

%(?..[%?] )

这是一个条件表达式.如果第一个点之前的表达式为真,则输出两个点之间的部分(在这种情况下没有).如果为false,则输出第二个点之后的部分.

It's a conditional expression. The part between the two dots (nothing in this case) is output if the expression before the first dot is true. The part after the second dot is output if it's false.

例如:

PS1='%n%m %~ %(?..[%?] )%# '

这篇关于显示前一个命令退出代码时自定义 zsh 的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆