# If you work with git, you've probably had that nagging sensation of not knowing what branch you are on. Worry no longer! | |
export PS1="\\w:\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\$ " | |
# This will change your prompt to display not only your working directory but also your current git branch, if you have one. Pretty nifty! | |
# ~/code/web:beta_directory$ git checkout master | |
# Switched to branch "master" | |
# ~/code/web:master$ git checkout beta_directory | |
# Switched to branch "beta_directory" | |
# ~/code/web:beta_directory$ |
This comment has been minimized.
This comment has been minimized.
__git_ps1 didn't work for me on my MacBook Pro. -bash: __git_ps1: command not found Resorted back to original post. |
This comment has been minimized.
This comment has been minimized.
Added the hostname export PS1="\h\w:$(git branch 2>/dev/null | grep '^*' | colrm 1 2)$ " |
This comment has been minimized.
This comment has been minimized.
Thanks man. export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\] \[\033[33;1m\]\w\[\033[m\] (\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)) \$ "
# gives me:
wayne ~/dev/lrn (master) $ # my name in green, curr dir in yellow, and branch in green Still a WIP. :) |
This comment has been minimized.
This comment has been minimized.
Try
Depending what directory you're in the line gets a bit long, this starts the actual in put on the line below. |
This comment has been minimized.
This comment has been minimized.
how to color the branch name ? |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Now that's a nice one @vankasteelj...(__git_ps1) doesn't work in my terminal on xubuntu so I substituted it with this gists original display method and removed the chevron symbol (personal preference)
|
This comment has been minimized.
This comment has been minimized.
How to change |
This comment has been minimized.
This comment has been minimized.
if __git_ps1 is not working take a look here: https://fedoraproject.org/wiki/Git_quick_reference |
This comment has been minimized.
This comment has been minimized.
I like this! __git_ps1 seems to force () around my branch and adds a space in front of it. Your option allows me to not have that which looks cleaner to me. If clutter in the ps1 string isn't desirable then folks always have the option of putting it in a bash function. git_branch() {
git branch 2>/dev/null | grep '^*' | colrm 1 2
}
export PS1="\$(git_branch)" Gone are the days of working on master by accident... Thanks again! |
This comment has been minimized.
This comment has been minimized.
The problem with gb() {
echo -n '(' && git branch 2>/dev/null | grep '^*' | colrm 1 2 | tr -d '\n' && echo -n ')'
}
git_branch() {
gb | sed 's/()//'
} It needs two functions because if we pipe everything into one line, the last sed command that strips the () doesn't receive the right input (notice the &&). |
This comment has been minimized.
This comment has been minimized.
I made a small modification to @vankasteelj version, with square brackets around the branch name:
|
This comment has been minimized.
This comment has been minimized.
Yet another variant, with a couple of nice features. First, I use PROMPT_COMMAND instead of PS1 to get coloured info, second I use GIT_PS1_SHOWDIRTYSTATE=1, GIT_PS1_SHOWCOLORHINTS=1,GIT_PS1_SHOWUNTRACKEDFILES=1 to turn on all the features. Also, with long prompt strings, you almost certainly want a newline in there near the end, so that there is plenty of space on each line. So putting it all together: export GIT_PS1_SHOWDIRTYSTATE=1 export PROMPT_COMMAND=' __git_ps1 "\n[\e[33m][[\e[m]\A [\e[31m]\u[\e[m]@[\e[32m]\h [\e[34;01m]\l[\e[m] [\e[36m]\w[\e[m]" "[\e[33m]][\e[m]\n$ "' which gives this sort of thing: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
With
|
This comment has been minimized.
This comment has been minimized.
@gordboy somehow I've got a LOT of additional redundant empty brackets using your set up | definitely I messed up on something :)
|
This comment has been minimized.
This comment has been minimized.
@skimpa can you share your PS1 i liked the style that you have created and i want to use the same one. |
This comment has been minimized.
This comment has been minimized.
@vankasteelj a year later still working like a charm on ubuntu 16.04, nicely done. thanks a lot |
This comment has been minimized.
This comment has been minimized.
My version (Updated) |
This comment has been minimized.
This comment has been minimized.
optional parenthesis |
This comment has been minimized.
This comment has been minimized.
Another version along with the status of your current branch (dirty, stashed content, etc) |
This comment has been minimized.
This comment has been minimized.
Using vankasteelj version on debian strech and working as expected. Thank you guys!!! |
This comment has been minimized.
This comment has been minimized.
Use a different color for the branch name? |
This comment has been minimized.
This comment has been minimized.
As an alternative, i wanted my Terminal (mac) window's title to display the branch as i navigated instead of the prompt. Added the https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh file to my home directory as .git-prompt-sh Added to my .bash_profile:
|
This comment has been minimized.
This comment has been minimized.
I used this:
it worked fine for me but I want to shorten the path to this : How can I do it !! |
This comment has been minimized.
This comment has been minimized.
@khadijanazih, use the following (from @giannidk and @vankasteelj): |
This comment has been minimized.
This comment has been minimized.
@vankasteelj some color commands are not needed, removing gives the following prompt
|
This comment has been minimized.
This comment has been minimized.
You could even color the git prompt depending on the state of your repository. For example, "Red" if there are any changes in the repo, "Green" otherwise, etc... |
This comment has been minimized.
This comment has been minimized.
We can now even copy paste emojis in the PS1 prompt string. Screenshot: |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@vankasteelj working on Debian 16.04 and it's working like a charm. Thank you. However this change is not permanent in my case, I have to reload bash_profile every time I open a new terminal. Any fix for that? |
This comment has been minimized.
This comment has been minimized.
Why aren't you using |
This comment has been minimized.
This comment has been minimized.
@hansfn, that is indeed better than |
This comment has been minimized.
This comment has been minimized.
@Sibasish19 you can add this to your ~/.bashrc instead if you'd like. |
This comment has been minimized.
This comment has been minimized.
vankasteelj wrote on Mar 7, 2016
Isn't there just too much going on? Even more unreadable than normal ;-) The following should be the same.
If you in addition remember that a color sticks until is changed or reset, we get:
I'm just mentioning this in case people copies vankasteelj code and tries to understand it ... PS! Reading Bash Prompt HOWTO: Colors and https://bash.cyberciti.biz/guide/Changing_bash_prompt is very useful. |
This comment has been minimized.
This comment has been minimized.
All I wanted is a simple, colored one-line prompt. @vankasteelj provided a nice one, I just extended it by not displaying brackets when outside a repo:
|
This comment has been minimized.
This comment has been minimized.
Is possible to add the current branch name on all the timestamp lines? Something like this: FROM:
TO:
Thanks. |
This comment has been minimized.
This comment has been minimized.
I like this one, thanks: PS1='[\033[0;32m][\033[0m\033[0;32m]\u[\033[0;36m] @ [\033[0;36m]\h \w[\033[0;32m]$(__git_ps1)\n[\033[0;32m]└─[\033[0m\033[0;32m] $[\033[0m\033[0;32m] |
This comment has been minimized.
This comment has been minimized.
I like this short directory formatting: function shortwd() {
num_dirs=3
pwd_symbol="..."
newPWD="${PWD/#$HOME/~}"
if [ $(echo -n $newPWD | awk -F '/' '{print NF}') -gt $num_dirs ]; then
newPWD=$(echo -n $newPWD | awk -F '/' '{print $1 "/.../" $(NF-1) "/" $(NF)}')
fi
echo -n $newPWD
}
git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1='\n\e[38;5;211m$(shortwd)\e[38;5;48m $(git_branch)\e[0m$'
export PS1 |
This comment has been minimized.
This comment has been minimized.
I like this better: Git Aware Prompt - https://github.com/jimeh/git-aware-promptexport GITAWAREPROMPT=~/.bash/git-aware-prompt PS1export PS1="[$txtblu]\w [$txtgrn]$git_branch[$txtred]$git_dirty[$txtrst]$ " I use iTerm2, so I can go into the preferences and change $txtblu with a nice UI color picker. |
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
how come none of these git branch 2> things are working.. the only way i get my branch is with git rev-parse --abbrev-ref HEAD... thankfully someone else showed that method...https://trueskawka.github.io/blog/programming/2018/01/17/git-branch-in-your-prompt.html |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@carinlynchin |
This comment has been minimized.
This comment has been minimized.
I used this, yet i can't figure out how to get it to leave out the [ ] if I'm not in a repo. PS1='[\033[1;32m][\033[0m\033[1;32m]\u[\033[1;33m] @ \w[\033[1;32m] - [$(git branch 2>/dev/null | grep "^*" | colrm 1 2)[\033[1;32m]][\033[0m\033[1;32m] $[\033[0m\033[1;32m][\033[0m] ' |
This comment has been minimized.
This comment has been minimized.
where is |
This comment has been minimized.
This comment has been minimized.
here is the tutorial for Windows |
This comment has been minimized.
This comment has been minimized.
ZSH + OMZ (Don't bash me for a non-bash answer)) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Here is Gargula, if anyone of you guys wants to give it a try: Gargula adds colors after a theme is defined and adds git status, git branch both are displayed only when git is initialized in a working directory. Gargula is displaying info that is useful to keep it as much minimalistic as possible. You can grab it here ---> https://github.com/auniverse4/gargula |
This comment has been minimized.
This comment has been minimized.
a slight color adjustment for some of the suggestions above. Show the branch if we're in a git repo. Show a brown repo name if there's no changes or red if there are changes in the repo.
|
This comment has been minimized.
This comment has been minimized.
Without sed: git_branch() { git describe --contains --all HEAD 2>/dev/null; } |
This comment has been minimized.
This comment has been minimized.
Here is a simple clean version that I use: link |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@archenroot so I really like your setup, but the git branch doesn't appear, and can you make it show the status too? thanks |
This comment has been minimized.
This comment has been minimized.
What I learnt:
So, I use |
This comment has been minimized.
This comment has been minimized.
I use the following code.
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks! |
This comment has been minimized.
This comment has been minimized.
I tried do this step, but it doesn't seem like I am still able to get a prompt, can you share your bash_profile? I mean does one need to do anything in bash_profile too?This is what I have in my bashrc
|
This comment has been minimized.
This comment has been minimized.
Hi, @wayneseymour. Your answer won't give what you said it would do. export PS1="\[\033[36m\]\u\[\033[m\]@\[\033[32m\] \[\033[33;1m\]\w\[\033[m\] \[\033[01;32m\]($(git branch 2>/dev/null | grep '^*' | colrm 1 2))\[\033[00m\] \$ |
This comment has been minimized.
This comment has been minimized.
@yzsatgithub makes sense! |
This comment has been minimized.
This comment has been minimized.
This one is pretty good. I am going to use it. Thank you Sir! |
This comment has been minimized.
This comment has been minimized.
ZSH (minimal - color/no-color + git information)
|
This comment has been minimized.
This comment has been minimized.
Thanks to @javieitez for the I lug my bash scripts around everywhere, gotta make them work for different environments. So here are some functions in case anybody wants to do similar. fnexists(){
declare -f $@ > /dev/null;
return $?;
}
if ! fnexists __git_ps1
then
__git_ps1() {
(echo -n '(' && git rev-parse --abbrev-ref HEAD 2> /dev/null | tr -d '\n' && echo -n ')') | sed 's/()//'
}
fi
windows() { [[ -n "$WINDIR" ]]; }
haspriv()
{
if windows;
then
net session > /dev/null 2>&1
if [ $? -eq 0 ]
then
return 0; #true
else
return 1; #false
fi
elif [ "$(whoami)" != "root" ]
then
return 1; #false
else
return 0; #true
fi
} this is how I actually setup my PS1 #esc="\033"
invert="\[\033[7m\]"
blink="\[\033[5m\]"
underlined="\[\033[4m\]"
dim="\[\033[2m\]"
bright="\[\033[1m\]"
clear="\[\033[0m\]"
cmdtime="\033[1;35m" # colors for trap DEBUG, timestamp before command runs
ps1time="\033[35m[\D{%a %T}]$clear" # ps1 timestamp in case I ever need that again
dir="\[\033[34m\]\w$clear"
branch="\[\033[34m\]$bright\`__git_ps1\`$clear"
login="\[\033[33m\]$bright\u@$clear\[\033[33m\]\h"
if haspriv
then
prompt="\[\033[33m\]#"
else
prompt="\[\033[33m\]$"
fi # $$clear was buggy
prompt="$prompt$clear"
place="$dir $branch"
#there is a single character unaccounted for, so I counted a space that shouldn't be counted - on the prompt line.
PS1="\[$clear\]\[\n$login $place\n\]$prompt\[ \]"
PS2="> " Final product looks something like this
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
da BEST! |
This comment has been minimized.
This comment has been minimized.
git-prompt.sh : https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh
|
This comment has been minimized.
This comment has been minimized.
parse_git_branch () { parse_git_remote () { get_date () { #export PS1="[[\033[32m]\w[\033[0m]]$(parse_git_branch)\n$(get_date) [\033[1;36m][\033[1;33m]-> [\033[0m]" |
This comment has been minimized.
This comment has been minimized.
This one seems simple and beautiful |
This comment has been minimized.
This comment has been minimized.
Or build your own with this nice graphical UI! |
This comment has been minimized.
This comment has been minimized.
@nitink66 try this one: export PS1="\u@[\033[32m]\w[\033[33m]$(git_branch)[\033[00m]$ " |
This comment has been minimized.
This comment has been minimized.
Stop to re-invent the wheel, git a try to liquidprompt |
This comment has been minimized.
This comment has been minimized.
Following video helped me add |
This comment has been minimized.
This comment has been minimized.
Interesting, thanks. When I'm putting that in my xterm, what is supposed to be the nice-looking This began to happen to my good old Debian (turned into a Devuan) a little while ago, some characters like ' or - are rendered as such a horrible square, and I can't figure out the reason why. (Yes, I know I'm a bit off-topic... Blame me.) |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks, this works well for me! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Windows 10 bash users :
|
This comment has been minimized.
This comment has been minimized.
I must say I have gotten addicted to the colours and calmness of PS1="\n\[\033[0;36m\]\u\[\033[0;31m\]@\[\033[0;32m\]\h \[\033[0;35m\]\$( git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(git:\1) /')\n\[\033[1;33m\]\w\[\033[00m\] \[\033[1;33m\]>\[\033[0;00m\] " Bonus: As its just a one-liner it's easy to put in scripts when you load a new vm echo PS1="\n\[\033[0;36m\]\u\[\033[0;31m\]@\[\033[0;32m\]\h \[\033[0;35m\]\$( git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(git:\1) /')\n\[\033[1;33m\]\w\[\033[00m\] \[\033[1;33m\]>\[\033[0;00m\] " >> ~.bashrc |
This comment has been minimized.
This comment has been minimized.
export PS1="\e[0;34m[\t] \e[m\e[0;32m\h: \e[m\e[0;33m\w: \e[m\e[0;35m$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\e[m\n$ " this is what I settled for... you'd probably wanna change the \h for \u to display the username |
This comment has been minimized.
This comment has been minimized.
This is my custom prompt I just spent a few hours putting together (total time from start to finish, but not total time spent working on it - I get distracted a lot rip). I have my terminal font set to Nerd Font to get the sweet icons in there. I decided to favor readability over compactness, to make it easier for future me if she ever wants to change it. # prompt
FMT_BOLD="\e[1m"
FMT_RESET="\e[0m"
FMT_UNBOLD="\e[21m"
FG_BLACK="\e[30m"
FG_BLUE="\e[34m"
FG_CYAN="\e[36m"
FG_GREEN="\e[32m"
FG_MAGENTA="\e[35m"
FG_RED="\e[31m"
FG_WHITE="\e[97m"
BG_BLUE="\e[44m"
BG_GREEN="\e[42m"
BG_MAGENTA="\e[45m"
export PS1=\
"\n ${FG_BLUE}╭─${FG_GREEN}${BG_GREEN} ${FMT_BOLD}${FG_RED} ${FG_BLACK}\u${FMT_UNBOLD} ${FG_GREEN}${BG_BLUE} "\
"${FG_BLACK}\w ${FMT_RESET}${FG_BLUE}"\
"\$(git branch 2> /dev/null | grep '^*' | colrm 1 2 | xargs -I BRANCH echo -n \"${BG_MAGENTA} ${FG_WHITE} BRANCH ${FMT_RESET}${FG_MAGENTA}\")"\
"\n ${FG_BLUE}╰ ${FG_CYAN}\$ ${FMT_RESET}" |
This comment has been minimized.
This comment has been minimized.
nice one cheesits456, I installed the nerd font but it looks like some of the fonts aren't rendering correctly, you must have all the nerd fonts installed, I only installed the Ubuntu nerd font. |
This comment has been minimized.
This comment has been minimized.
i also only installed the ubuntu font - each nerd font contains exactly the same symbols as each other - they're all just patched versions of standard fonts. as far as the symbols/icons are concerned, every nerd font is identical |
This comment has been minimized.
This comment has been minimized.
Here is mine, just collected from above comments and custom a bit with cygpath to make it become similar to windows. Thanks @vankasteelj and @cheesits456. |
This comment has been minimized.
This comment has been minimized.
I don't have
|
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
oh nice, I didn't know bash supported the |
This comment has been minimized.
This comment has been minimized.
Just gonna post this update, its been a while since my last addition # prompt
FMT_BOLD="\[\e[1m\]"
FMT_DIM="\[\e[2m\]"
FMT_RESET="\[\e[0m\]"
FMT_UNBOLD="\[\e[22m\]"
FMT_UNDIM="\[\e[22m\]"
FG_BLACK="\[\e[30m\]"
FG_BLUE="\[\e[34m\]"
FG_CYAN="\[\e[36m\]"
FG_GREEN="\[\e[32m\]"
FG_GREY="\[\e[37m\]"
FG_MAGENTA="\[\e[35m\]"
FG_RED="\[\e[31m\]"
FG_WHITE="\[\e[97m\]"
BG_BLACK="\[\e[40m\]"
BG_BLUE="\[\e[44m\]"
BG_CYAN="\[\e[46m\]"
BG_GREEN="\[\e[42m\]"
BG_MAGENTA="\[\e[45m\]"
PS1="\n ${FG_BLUE}╭─" # begin arrow to prompt
PS1+="${FG_MAGENTA}" # begin USERNAME container
PS1+="${BG_MAGENTA}${FG_CYAN}${FMT_BOLD} " # print OS icon
PS1+="${FG_WHITE}\u" # print username
PS1+="${FMT_UNBOLD} ${FG_MAGENTA}${BG_BLUE} " # end USERNAME container / begin DIRECTORY container
PS1+="${FG_GREY}\w " # print directory
PS1+="${FG_BLUE}${BG_CYAN} " # end DIRECTORY container / begin FILES container
PS1+="${FG_BLACK}"
PS1+=" \$(find . -mindepth 1 -maxdepth 1 -type d | wc -l) " # print number of folders
PS1+=" \$(find . -mindepth 1 -maxdepth 1 -type f | wc -l) " # print number of files
PS1+=" \$(find . -mindepth 1 -maxdepth 1 -type l | wc -l) " # print number of symlinks
PS1+="${FMT_RESET}${FG_CYAN}"
PS1+="\$(git branch 2> /dev/null | grep '^*' | colrm 1 2 | xargs -I BRANCH echo -n \"" # check if git branch exists
PS1+="${BG_GREEN} " # end FILES container / begin BRANCH container
PS1+="${FG_BLACK} BRANCH " # print current git branch
PS1+="${FMT_RESET}${FG_GREEN}\")\n " # end last container (either FILES or BRANCH)
PS1+="${FG_BLUE}╰ " # end arrow to prompt
PS1+="${FG_CYAN}\\$ " # print prompt
PS1+="${FMT_RESET}"
export PS1 |
This comment has been minimized.
This comment has been minimized.
@cheesits456 I really like yours, although it is a bit off in directories that aren't git, shows a lot of Chinese like markings, (lool and some numbers too, can I know what they stand for ? thanks ). |
This comment has been minimized.
This comment has been minimized.
its because im using nerdfont, it replaces those characters with icon glyphs |
This comment has been minimized.
This comment has been minimized.
I edited a bit @vankasteelj's stuff and made something like zsh, to react to git's changes function changes_in_branch() {
if [ -d .git ]; then
if expr length + "$(git status -s)" 2>&1 > /dev/null; then
echo -ne "\033[0;33m$(__git_ps1)\033[0m";
else
echo -ne "\033[0;32m$(__git_ps1)\033[0m"; fi;
fi
} and then simply just add that function to the PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\] @ \[\033[0;36m\]\h \w\[\033[0m\]$(changes_in_branch)\n\[\033[0;32m\]└─\[\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] ▶\[\033[0m\] ' |
This comment has been minimized.
This comment has been minimized.
Works! Thank you! Ubuntu 20.4 |
This comment has been minimized.
This comment has been minimized.
@stealthman22 I just realized I never actually gave a proper answer to your question, sorry :P Nerd Font is a project that makes patched versions of popular fonts, replacing a ton of unused characters with icons - you can download one from https://www.nerdfonts.com/font-downloads, then put the ttf file in As for what they represent, I'll go left to right. The one that looks like a purple Here's a screenshot that shows the relation between the numbers and the types of files in the current directory: Hope this helps <3 |
This comment has been minimized.
This comment has been minimized.
Here is mine PS1:
|
This comment has been minimized.
Why not simply use the __git_ps1 function ?