Strange behavior in mac os x terminal

I started using this code from Mark Dotto ( http://markdotto.com/2013/01/13/improved-terminal-hotness/ ) to make my terminal look sexier. I just copied the code without editing it, so in mine .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 works, but there is a strange thing: when I print 3 characters or less, I press backspace, it removes everything, even the information on the left (git path and branch). This may be fine, but the problem is that when I continue typing after this, the command I started typing is still here (but hidden). I think you misunderstood, so I'll try to show the 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 absolutely know how this bash_profile works, can anyone help? Thanks to

+3


source to share


2 answers


incorrect syntax appears in your PS1 variable causing some unexpected errors. try this revision:

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

      

(note: I left only the pipeline git ... grep ... sed

and only edited the parts related to the tooltip itself.)

edit - take out the parts 0:

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 the foreground text color (light blue / tealish)
  • \w

    - current working directory
  • \[\e[33m\]

    - sets a different text color (yellow)
  • git ... grep ... sed

    - returns the current git branch
  • \[\e[0m\]

    - resets text color to white so you don't print commands in yellow

if you don't like colors, hints are pretty trivial. color blocks make it a little more complex and, as you've seen, are prone to errors.

+2


source


First things first: make sure you are using the BASH shell.

I'm on Mountain Lion on a MacBook and the PS1 team is kinda kind. My prompt looks like this:

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

      

I think the question is, do you want your request to be fulfilled. BASH hints can insert a whole bunch of escape sequences, which can do all sorts of neat things that a bit of hack in Kornshell could take with them.

Type man bash

in the command line and find the heading PROMPTING. 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 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 query. I want to show my username, the system I'm on, and the current directory. I can install PS1 like this:

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

      

This will give me:

 david@davebook:~$ _

      

\u

is my username (david), \h

is my machine name (davebook), and \w

displays the current directory which I refer to my directory $HOME

.



You can also paste commands into the tooltip:

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:~

      

Sorting silly (I had to format the date. Also, BASH already built sequences for the date), but you get the idea.

I recommend that you create your own damn invitation. If you are a user git

and you use comfortable command lines, you can probably make a nice prompt to see how you want. The syntax \$(command)

can be used to enable interactive commands that are executed with each new PS command. You can use ANSI escape codes to color different parts of your invitation or make them look fantastic.

Build your invitation slowly and in stages. Create a wrapper script that will install PS1

and output it like this:

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

      

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

Personally, I avoid fantastic tips simply because they tend to fall apart sometime when you least expect them to. For example, I use VI sequences for editing, and this hint is just completely separated from each other whenever I try to change my command line.

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

+3


source







All Articles