Pregunta

I started using this code from Mark Dotto (http://markdotto.com/2013/01/13/improved-terminal-hotness/) to make my terminal is bit sexier. I just copied the code without editing it, so in my .bash_profile I added:

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\]'

Everything's working, but there is a weird thing: when I type 3 characters or less then I hit backspace, it deletes everything, even the informations on the left (the path and git branch). This could be okay, but the problem is that when I keep typing after that, the command I started typing is still here (but hidden). I guess you didn't understand so I'll try to show some code:

# 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

I have absolutely know idea how this bash_profile works, anybody can help? Thanks

¿Fue útil?

Solución

there appears to be some incorrect syntax in your PS1 variable that's causing some unexpected errors. try this revision instead:

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

(note: i left the git ... grep ... sed pipeline alone and only edited the parts related to the prompt itself.)

edit - take out the 0: parts and the colors actually work. (i.e. \[\e[36m\] instead of \[\e[0:36m\])

and here's a breakdown of what's going on there:

  • \[\e[36m\] - this block sets a foreground text color (light blue/tealish)
  • \w - current working directory
  • \[\e[33m\] - sets a different text color (yellow)
  • git ... grep ... sed - retrieves your current git branch
  • \[\e[0m\] - resets the text color to white so you're not typing commands in yellow

if you don't care about colors, prompts are a fairly trivial thing. the color blocks make it a bit more complex, and (as you've seen) error prone.

Otros consejos

First of all: Make sure you are using the BASH shell.

I am on Mountain Lion on a MacBook and the PS1 command sort of, kind of works. My prompt looks like this:

⌘ ~/SVN-Precommit-Kitchen-Sink-Hook.git/ (master) _

I guess the question is what do you want your prompt to do. BASH prompts can embed a whole bunch of escape sequences that can do all sorts of neat things that in Kornshell would take a wee bit of hacking.

Type man bash on the command line, and find the PROMPTING heading. You should see something like this:

   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

Let's take a simple prompt. I want to display my user name, the system I'm on, and my current directory. I can set PS1 like this:

PS1="\u@\h:\w$ "

This will give me:

 david@davebook:~$ _

The \u is my user name (david), the \h is my machine name (davebook), and the \w displays the current directory I'm in relation to my $HOME directory.

You can also embed commands in the prompt too:

PS1="\$(date) \u@\h:\w$ "

Now the date and time will be embedded in my prompt:

Fri Feb  1 09:45:53 EST 2013 david@DaveBook:~

Sort of silly (I should have formatted the date. Besides, BASH already has built in sequences for the date), but you get the idea.

I recommend that you build your own damn prompt. If you're a git user, and you're using command lines comfortably, you can probably make a nice prompt yourself to look the way you want. You can use the \$(command) syntax to include interactive commands that get executed with each new PS command. You can use ANSI escape codes to color different parts of your prompt, or make them do fantastic stuff.

Build your prompt slowly and bit-by-bit. Create a shell script that will set PS1, and source it in like this:

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

Then, add more and more features to your prompt until you get it to work the way you want.

Personally, I avoid over fancy prompts simply because they tend to fall apart sometime when you least expect it. For example, I use VI sequences for editing, and that prompt simply falls completely apart whenever I try to edit my command line.

Fancy prompts remind me of programs like Talking Moose which are really cool for the first few minutes, then start getting really, really annoying after that.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top