2

I would like to edit my shell prompt to display time. I am currently using zsh, though I would like the customize the shell prompt to show time.

I understand that an echo $PS1 will give me the environment variable contents. Given:

[%n]%~ 

Setting the prompt using:

setenv PROMPT '[%n]%t ' 

I also know if I type man zsh and press enter it will give me a list of formats I can change to.

1
  • setenv is a csh command. You should get a "command not found" error in zsh. Commented Jun 19, 2015 at 13:32

2 Answers 2

4

I think you want to do:

PROMPT='[%n]%t ' 

setenv is a csh command. The equivalent in Bourne-like shell would be export, but here you don't want to export that variable to the environment, just define the $PROMPT shell variable (typically in your ~/.zshrc).

Try man zshmisc and look for the section "SIMPLE PROMPT ESCAPES" (or run info zsh 'prompt expansion') to see all of the escape-sequences available. Also, take a look at zsh's RPROMPT variable. It can be useful and fun.

3
  • I read the RPROMPT is good for displaying time as well, it shows on the right side of the terminal or screen right? Commented Jun 19, 2015 at 16:05
  • I decided on bash, which I could add time by setting the environment variable, PS1="\s-\v-\t\$" though strange, the prompt does not allow me to type on the same line as the prompt, actually below it. Commented Jun 25, 2015 at 19:57
  • Also, what you have suggested also works. Commented Jun 25, 2015 at 20:00
2

If you want to include the system time in the zsh prompt here is the secret sauce:

  • %T for the current time in 24-hour format => 13:42
  • %t for the time to appear in AM/PM or 12-hour format, or use => 1:42 PM
  • %* to display the time in the 24-hour format along with seconds. => 13:42:03

I would highly recommend using RPROMPT to display the value on the right side of the terminal.

I have RPROMPT="[%D{%Y/%m/%f}|%*]"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.