如何在 zsh 中为命令提示符设置 dd/mm/yy?

如何在 zsh 中为命令提示符设置 dd/mm/yy?

我使用 zsh (不使用 oh-my-zsh)并且想要一个自定义命令提示符。

目前它只提供主机名和 % 作为命令提示符 -

[code]
think-debian% hostname
think-debian
[/code]

我想做的是——

[username/userid@hostname] - [pwd]-[ DD/MM/YY local time in hh:mm:ss] $

我该如何处理?

我确实在 man zshmisc 中看到了线索

特别是在提示序列的扩展中

提示序列的扩展 提示序列经历特殊形式的扩展。使用内置 print 的 -P 选项也可以实现这种类型的扩展。

   If the PROMPT_SUBST option is set, the prompt string is first subjected to parameter expansion, command substitution and arithmetic 

扩张。请参见 zshexpn(1)。

   Certain escape sequences may be recognised in the prompt string.

   If  the  PROMPT_BANG  option is set, a `!' in the prompt is replaced by the current history event number.  A literal `!' may then

被表示为“!!”。

   If the PROMPT_PERCENT option is set, certain escape sequences that start with `%' are expanded.  Many escapes  are  followed  by  a 

单个字符,尽管其中一些采用可选的整数参数,该参数应出现在“%”和序列的下一个字符之间。更复杂的转义序列可用于提供条件扩展。

简单提示转义字符 %% A `%'。

   %)     A `)'.

登录信息 %l 用户登录的线路 (tty),没有/dev/' prefix. If the name starts with/dev/tty',该前缀被去除。

   %M     The full machine hostname.

%m 直到第一个.'. An integer may follow the %' 的主机名,用于指定需要多少个主机名组成部分。如果是负整数,则显示主机名的尾部部分。

   %n     $USERNAME.

   %y     The line (tty) the user is logged in on, without `/dev/' prefix.  This does not treat `/dev/tty' names specially.

如果不是,则shell 状态 %# A #' if the shell is running with privileges, a%'。出于这些目的,与%(!.#.%%)'. The definition of“特权”等效的是有效用户 ID 为零,或者如果支持 POSIX.1e 功能,则在有效或可继承功能向量中至少提出一种功能。

   %?     The return status of the last command executed just before the prompt.

   %_     The  status  of  the parser, i.e. the shell constructs (like `if' and `for') that have been started on the command line. If

给定一个整数,将打印许多字符串;零或负数或无整数意味着打印尽可能多的值。这在提示 PS2 连续行和 PS4 使用 XTRACE 选项进行调试时最有用;在后一种情况下,它也将以非交互方式工作。

%^ 反向解析器的状态。除了字符串的顺序之外,这与“%_”相同。常用于RPS2。

   %d
   %/     Current working directory.  If an integer follows the `%', it specifies a number of  trailing  components  of  the  current 

要显示的工作目录;零表示整个路径。负整数指定前导组件,即 %-1d 指定第一个组件。

日期和时间 %D yy-mm-dd 格式的日期。

   %T     Current time of day, in 24-hour format.

   %t
   %@     Current time of day, in 12-hour, am/pm format.

   %*     Current time of day in 24-hour format, with seconds.

   %w     The date in day-dd format.

   %W     The date in mm/dd/yy format.

我尝试了很多方法,但无法在 dd/Month/yy 中获取它 -

think-debian%PS1=%n@%m-%/\ %D\ %*\ $

shirish@think-debian-/home/shirish 16-12-19 12:33:27 $ PS1=%n@%m-%/\ %W\ %*\ $

shirish@think-debian-/home/shirish 12/19/16 12:33:55 $

更新 - 已经非常接近 -

$PS1=%n@%m-%/\ %D{%d/%m/%y}\ %*\ $
 shirish@think-debian-/home/shirish 19/12/16 17:31:09 $ 

期待知道

运行 zsh 版本 5.2

答案1

对于格式中的日期/时间部分DD/MM/YY HH:MM:SS,您可以使用以下内容:

%D{%d/%m/%y %H:%M:%S}

参考

%D{string}:使用 strftime 函数格式化字符串。 ...

相关内容