4

我开始使用 Mark Dotto ( http://markdotto.com/2013/01/13/improved-terminal-hotness/ ) 的这段代码来让我的终端更性感一些。我只是复制了代码而没有编辑它,所以在我的.bash_profile添加:

export PS1='\[\e[0:35m⌘\e[m \e[0:36m\w/\e[m \e[0:33m`git branch 2> /dev/null | grep -e ^* | sed -E  s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`\e[m\]'

一切正常,但有一件奇怪的事情:当我输入 3 个或更少的字符然后按退格键时,它会删除所有内容,甚至是左侧的信息(路径和 git 分支)。这可能没问题,但问题是当我在那之后继续输入时,我开始输入的命令仍然在这里(但隐藏)。我猜你不明白,所以我会尝试显示一些代码:

# this is what my prompt looks like
~/my/path/ (branch) |

# I start typing a command
~/my/path/ (branch) ls|

# now I hit backspace once
|

# everything is removed
# but if I type something else then hit return
git st|

# it throws an error as the `l` from the previous command is still here
-bash: lgit: command not found

我完全知道这个 bash_profile 是如何工作的,有人可以帮忙吗?谢谢

4

2 回答 2

3

您的 PS1 变量中似乎存在一些不正确的语法,导致一些意外错误。试试这个版本:

export PS1='\[\e[36m\]\w \[\e[33m\]`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /` \[\e[0m\]'

(注意:我单独留下了git ... grep ... sed管道,只编辑了与提示本身相关的部分。)

编辑 - 取出0:零件和颜色实际工作。(即\[\e[36m\]代替\[\e[0:36m\]

这是那里发生的事情的细分:

  • \[\e[36m\]- 此块设置前景文本颜色(浅蓝色/蓝绿色)
  • \w- 当前工作目录
  • \[\e[33m\]- 设置不同的文本颜色(黄色)
  • git ... grep ... sed- 检索你当前的 git 分支
  • \[\e[0m\]- 将文本颜色重置为白色,这样您就不会以黄色输入命令

如果您不关心颜色,提示是一件相当琐碎的事情。颜色块使它更复杂一些,并且(如您所见)容易出错。

于 2013-02-01T14:38:14.677 回答
3

首先:确保您使用的是 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这样的程序,前几分钟真的很酷,然后开始变得非常非常烦人。

于 2013-02-01T15:01:09.220 回答