mac os x终端中的奇怪行为

时间:2013-02-01 14:04:52

标签: macos bash terminal .bash-profile

我开始使用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是如何工作的,任何人都可以提供帮助?感谢

2 个答案:

答案 0 :(得分: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 escape codes为提示的不同部分着色,或者让它们做很棒的事情。

慢慢地逐步建立你的提示。创建一个将设置PS1的shell脚本,并将其源化为:

$ echo "PS='\u@\h:\w\$ " > prompt.sh
$ chmod a+x prompt.sh
$ . prompt.sh

然后,在您的提示中添加越来越多的功能,直到您按照自己的方式工作。

就我个人而言,我避免过于花哨的提示,因为它们往往会在你最不期望的时候崩溃。例如,我使用VI序列进行编辑,每当我尝试编辑命令行时,该提示就会完全分开。

花哨的提示让我想起像Talking Moose这样的程序,这些程序在最初的几分钟内非常酷,然后开始变得非常非常烦人。

答案 1 :(得分:2)

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\] - 将文字颜色重置为白色,这样就不会输入黄色命令

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