首先:确保您使用的是 BASH shell。
我在 MacBook 上的 Mountain Lion 和 PS1 命令之类的工作。我的提示如下所示:
⌘ ~/SVN-Precommit-Kitchen-Sink-Hook.git/ (master) _
我想问题是你想让你的提示做什么。BASH 提示可以嵌入一大堆转义序列,这些转义序列可以做各种巧妙的事情,在 Kornshell 中需要一点点黑客攻击。
man bash
在命令行上键入,然后找到 PROMPTING 标题。您应该看到如下内容:
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a com-
mand, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these
prompt strings to be customized by inserting a number of backslash-escaped special characters that
are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}
the format is passed to strftime(3) and the result is inserted into the prompt string;
an empty format results in a locale-specific time representation. The braces are
required
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\@ the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated with a tilde
\W the basename of the current working directory, with $HOME abbreviated with a tilde
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could be used to embed a terminal
control sequence into the prompt
\] end a sequence of non-printing characters
让我们做一个简单的提示。我想显示我的用户名、我所在的系统和我的当前目录。我可以这样设置 PS1:
PS1="\u@\h:\w$ "
这会给我:
david@davebook:~$ _
是我的\u
用户名 (david),\h
是我的机器名 (davebook), 并\w
显示与我的目录相关的当前$HOME
目录。
您也可以在提示符中嵌入命令:
PS1="\$(date) \u@\h:\w$ "
现在日期和时间将嵌入到我的提示中:
Fri Feb 1 09:45:53 EST 2013 david@DaveBook:~
有点傻(我应该格式化日期。此外,BASH 已经内置了日期序列),但你明白了。
我建议你建立自己的该死的提示。如果您是git
用户,并且您正在舒适地使用命令行,那么您可能会自己做出一个很好的提示,以按照您想要的方式进行查看。您可以使用\$(command)
语法来包含与每个新 PS 命令一起执行的交互式命令。您可以使用ANSI 转义码为提示的不同部分着色,或者让它们做一些很棒的事情。
慢慢地、一点点地构建你的提示。创建一个将设置的 shell 脚本PS1
,并像这样输入它:
$ echo "PS='\u@\h:\w\$ " > prompt.sh
$ chmod a+x prompt.sh
$ . prompt.sh
然后,将越来越多的功能添加到您的提示中,直到您让它以您想要的方式工作。
就个人而言,我避免过于花哨的提示仅仅是因为它们往往会在你最不期望的时候分崩离析。例如,我使用 VI 序列进行编辑,但每当我尝试编辑命令行时,该提示就会完全崩溃。
花哨的提示让我想起了像Talking Moose这样的程序,前几分钟真的很酷,然后开始变得非常非常烦人。