I'm using Mac. I want to set PS1 variable to get current time at the end of line in command line prompt. Like this: or this:
我正在使用Mac。我想设置PS1变量以在命令行提示符下的行尾获取当前时间。像这样:或者这个:
But, unfortunately, I can't get what I want through following configuration:
但是,遗憾的是,通过以下配置我无法得到我想要的东西:
CYAN="\[$(tput setaf 6)\]"
GREEN="\[$(tput setaf 2)\]"
BLUE="\[$(tput setaf 4)\]"
YELLOW="\[$(tput setaf 3)\]"
export PS1="$GREEN\u$BLUE@$CYAN\h $YELLOW\w\[$(tput cuf $(($(tput cols)-33)))\]$CYAN\[$(date +%H:%M)\]\n\\$ "
It seems that I need to get the current position of cursor and do some computations based on the coordinate of the position, but I don't know how to get it. Is anyone can help?
似乎我需要获取光标的当前位置并根据位置的坐标进行一些计算,但我不知道如何获得它。有人可以帮忙吗?
2 个解决方案
#1
There are a few issues. This chunk
有一些问题。这块
$(tput cuf $(($(tput cols)-33)))
appears to be what you are asking about. If you want to position the date at the end of the line, you might try using the hpa
(horizontal position absolute) capability, e.g.,
似乎是你在问什么。如果要将日期定位在行尾,可以尝试使用hpa(水平位置绝对)功能,例如,
$(tput hpa $(($(tput cols)-5)))
That way, you can move the cursor to a given absolute position on the line rather than moving relative to the current position.
这样,您可以将光标移动到线上的给定绝对位置,而不是相对于当前位置移动。
The prompt as given occupies two lines. If you use the sc
and rc
capabilities (save/restore cursor) before and after the part where you write the date, you can reduce that to a single line. That might run into problems with line-editing clearing the date, but it is something to consider.
给定的提示占用两行。如果在编写日期的部分之前和之后使用sc和rc功能(保存/恢复光标),则可以将其减少到一行。这可能会遇到行清除日期的问题,但需要考虑。
#2
Thanks @Thomas for the good answer, it can truly solve the question I asked as a work-around solution. But it is not perfect, which means it still cannot get the command prompt that I want.
感谢@Thomas的好答案,它可以真正解决我作为解决方案提出的问题。但它并不完美,这意味着它仍然无法获得我想要的命令提示符。
I found that there should be no solution for the prompt I want in bash. But, as @chepner said, there do exist methods to get the prompt in zsh.
我发现在bash中我想要的提示应该没有解决方案。但是,正如@chepner所说,确实存在在zsh中获取提示的方法。
How to get the prompt in zsh:
如何在zsh中获取提示:
zsh is a very powerful shell which is built-in in mac, but it's a little complicate for the newbie to it. So there is a great tools to solve it named "oh-my-zsh" which make the use of zsh much easier.
zsh是一个非常强大的shell,它内置于mac中,但它对于新手来说有点复杂。所以有一个很棒的工具可以解决它名为“oh-my-zsh”的问题,它使zsh的使用变得更加容易。
"oh-my-zsh" include many themes for zsh. And, fortunately, the prompt I want is very similar with one of them(see the themes here), which named blink. So I just need to modify a little of the theme file located in ~/.oh-my-zsh/themes/blinks.zsh-theme
and the change needed to do is:
“oh-my-zsh”包含zsh的许多主题。而且,幸运的是,我想要的提示与其中一个提示非常相似(请参阅此处的主题),其名称为blink。所以我只需要修改位于〜/ .oh-my-zsh / themes / blinks.zsh-theme中的一些主题文件,需要做的更改是:
change the last line of the file from:
从以下位置更改文件的最后一行:
RPROMPT='%{%B%F{cyan}%}%!%{%f%k%b%}
to
RPROMPT='%{%B%F{cyan}%}%@%{%f%k%b%}
#1
There are a few issues. This chunk
有一些问题。这块
$(tput cuf $(($(tput cols)-33)))
appears to be what you are asking about. If you want to position the date at the end of the line, you might try using the hpa
(horizontal position absolute) capability, e.g.,
似乎是你在问什么。如果要将日期定位在行尾,可以尝试使用hpa(水平位置绝对)功能,例如,
$(tput hpa $(($(tput cols)-5)))
That way, you can move the cursor to a given absolute position on the line rather than moving relative to the current position.
这样,您可以将光标移动到线上的给定绝对位置,而不是相对于当前位置移动。
The prompt as given occupies two lines. If you use the sc
and rc
capabilities (save/restore cursor) before and after the part where you write the date, you can reduce that to a single line. That might run into problems with line-editing clearing the date, but it is something to consider.
给定的提示占用两行。如果在编写日期的部分之前和之后使用sc和rc功能(保存/恢复光标),则可以将其减少到一行。这可能会遇到行清除日期的问题,但需要考虑。
#2
Thanks @Thomas for the good answer, it can truly solve the question I asked as a work-around solution. But it is not perfect, which means it still cannot get the command prompt that I want.
感谢@Thomas的好答案,它可以真正解决我作为解决方案提出的问题。但它并不完美,这意味着它仍然无法获得我想要的命令提示符。
I found that there should be no solution for the prompt I want in bash. But, as @chepner said, there do exist methods to get the prompt in zsh.
我发现在bash中我想要的提示应该没有解决方案。但是,正如@chepner所说,确实存在在zsh中获取提示的方法。
How to get the prompt in zsh:
如何在zsh中获取提示:
zsh is a very powerful shell which is built-in in mac, but it's a little complicate for the newbie to it. So there is a great tools to solve it named "oh-my-zsh" which make the use of zsh much easier.
zsh是一个非常强大的shell,它内置于mac中,但它对于新手来说有点复杂。所以有一个很棒的工具可以解决它名为“oh-my-zsh”的问题,它使zsh的使用变得更加容易。
"oh-my-zsh" include many themes for zsh. And, fortunately, the prompt I want is very similar with one of them(see the themes here), which named blink. So I just need to modify a little of the theme file located in ~/.oh-my-zsh/themes/blinks.zsh-theme
and the change needed to do is:
“oh-my-zsh”包含zsh的许多主题。而且,幸运的是,我想要的提示与其中一个提示非常相似(请参阅此处的主题),其名称为blink。所以我只需要修改位于〜/ .oh-my-zsh / themes / blinks.zsh-theme中的一些主题文件,需要做的更改是:
change the last line of the file from:
从以下位置更改文件的最后一行:
RPROMPT='%{%B%F{cyan}%}%!%{%f%k%b%}
to
RPROMPT='%{%B%F{cyan}%}%@%{%f%k%b%}