What I should have is:
PS1=”: \u@\h\$ ;”
Because that means I can simply cut&paste a whole line and it is still a valid command. Triple-clicking (in xterm, at least) is an easy-ish way to get a whole line, so…
]]>Sorry, one more… I was getting weird linewrap issues using the above code (illustrative GIF: http://i.stack.imgur.com/fccjc.gif). Because I had my color codes defined as:
##-ANSI-COLOR-CODES-##
Color_Off="\[33[0m\]"
###-Regular-###
Red="\[33[0;31m\]"
Green="\[33[0;32m\]"
Purple="\[33[0;35\]"
Yellow="\[33[1;33m\]"
####-Bold-####
BRed="\[33[1;31m\]"
BPurple="\[33[1;35m\]"
lines like
PS1+="\[$Green\][\!]\[$Color_Off\] "
were causing the escape sequences to be double quoted. The article’s a little unclear as to the final color definitions you arrived at (it also doesn’t define $Yellow despite its use in the SSH_CLIENT code). If somebody’s reading this and defines their colors as I do, I’ll save you the trouble of removing the extra escape sequences:
@ captnjlp:
# set up command prompt
function __prompt_command()
{
# capture the exit status of the last command
EXIT="$?"
PS1=""
if [ $EXIT -eq 0 ]; then PS1+="$Green[\!]$Color_Off "; else PS1+="$Red[\!]$Color_Off "; fi
# if logged in via ssh shows the ip of the client
if [ -n "$SSH_CLIENT" ]; then PS1+="$Yellow(${SSH_CLIENT%% *})$Color_Off"; fi
# debian chroot stuff (take it or leave it)
PS1+="${debian_chroot:+($debian_chroot)}"
# basic information (user@host:path)
PS1+="$BRed\u$Color_Off@$BRed\h$Color_Off:$BPurple\w$Color_Off "
# check if inside git repo
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
# parse the porcelain output of git status
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local Color_On=$Green
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local Color_On=$Purple
else
local Color_On=$Red
fi
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
branch=${BASH_REMATCH[1]}
else
# Detached HEAD. (branch=HEAD is a faster alternative.)
branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null || echo HEAD`)"
fi
# add the result to prompt
PS1+="$Color_On[$branch]$Color_Off "
fi
# prompt $ or # for root
PS1+="\$ "
}
PROMPT_COMMAND=__prompt_command
Thanks for posting this; saved me inordinate amounts of time. However, the SSH_CLIENT line creates a “bad substitution” error on every prompt. Removing the extra dollar sign (and the unescaped quotation marks) fixed it for me:
if [ -n "$SSH_CLIENT" ]; then PS1+="\[$Yellow\](${SSH_CLIENT%% *})\[$Color_Off\]"; fi
Color me unimpressed. The lengths some hipsters will go to in order to feel unique. A prompt is supposed to be functional, not distracting.
]]>Brilliant look for the PS1, love the idea.
Just an FYI for people ending up here using Ubuntu.
When I tried to use the above it didn’t work for me (Ubuntu 13.10). This is because Ubuntu has been using a different shell since 6.10 (https://wiki.ubuntu.com/DashAsBinSh).
Basically it means that some of the things (like the substitutions) wont work. I haven’t figured out how to get it to adjust the above code to make it work. But if someone does: post it!
Hey!
I’ve fighted hours with the bad wrapping of lines due to the use of colors in PS1.
The best solution I found:
https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=14800
And yes the solution is as simple as using 01 and 02 instead of \[ and \]!!!!
Unfortunately, it is very difficult to find, since it is not discussed in bash texts/blogs/articles/…, but in an R lang bug report!!!
Hope this helps!
]]>Why don’t you use some of the other git ps1 vars like:
GIT_PS1_SHOWDIRTYSTATE
GIT_PS1_SHOWUPSTREAM
Check them out here
]]>Checking for exit status 0 isn’t enough; when you type Ctrl+Z to push a command into the background (“stop” it, in POSIX terms), $? gets set to a non-zero value. The solution is to check for
[ $? -eq 0 -o `kill -l $?` = TSTP ]
I’m often confronted with a lack of screen space, so I tend to go with the minimalistic:
PS1="\$ "
as it means you don’t have anything taking up valuable console width.
I found a nifty little trick that will only print the full prompt if you hit enter twice in a row or when opening a new terminal. Gives more room on the line for the rest of your commands.
.bashrc snippet here: http://sprunge.us/OQjU
git-completion-plus.sh: http://sprunge.us/SbRZ
This adds a nice indication as to what git branch you’re on, how many uncommitted changes and committed changes there are and the current path/hostname. The branch name and the number of changes all change color depending on the status. I like your idea of adding the IP address for remote servers. I only access a few different machines, so I just use different colors for my tmux status bar depending on which machine I’m logged in to (95% of the time I have tmux running).
firecat53@scotty:~/projects/company/inventory/src (master) 1 1 $
Scott
]]>