-
Star
(389)
You must be signed in to star a gist -
Fork
(72)
You must be signed in to fork a gist
-
-
Save justintv/168835 to your computer and use it in GitHub Desktop.
# 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$ |
PS1="\[\e[0;1;34m\]\W \[\e[0;1;33m\]\$(git branch 2>/dev/null | grep '^*' | colrm 1 2)\[\e[0;7;0;37m\]$ "
Try this, for minimal text on terminal, with present working directory in blue and branch in yellow color
[...]
git branch 2>/dev/null | grep '^*' | colrm 1 2
[...]
Or with a newer Git:
git branch --show-current 2>/dev/null
It's not shorter, but easier to understand ;-)
git branch --show-current 2>/dev/null
Is not working with newer git
This has been supported since at least Git 2.28.0, but let us not spend any more time on this. Your solution works for all versions :-)
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] ▶[\033[0m] '
Excellent! Thanks!
Great collection!
Any way to make the path part of the prompt git-aware?
I'd want it to behave similarly to how it is homedir aware, with highlighting via different colors git's rootdir (not submodule's rootdir, mind you) and the rest of the path
Sort of like this: ~/path/to/GITROOT (BRANCH) path/in/tree
if not even GITROOT (BRANCH) path/in/tree
ignoring path to the root
edit: ended up piecing a few things together and overriding the prompt function with an external script.
Careful, even if a lot of people above are talking ZSH, I'm doing bash things here as the original author of the gist did too
Using this stackoverflow answer and this one as basis and sourcing /usr/share/git/git-prompt.sh (path will be different on other distros) above this function
The path section ended up being:
# time to manipulate path, let's check if path is in git
local git_top=$(git rev-parse --show-toplevel 2>/dev/null)
if [ "$git_top" != "" ]; then
# we're in git
# substitute homedir
git_relative_top=${git_top/$HOME/\~}
reponame=${git_top##*/}
# green non-bold path to repo
PS1+="\[$txtreset\]\[$txtgreen\]${git_relative_top%$reponame}"
# repo itself is bold
PS1+="\[$txtbold\]$reponame\[$txtreset\]"
# now the branch info in blue
# apply the git ps1 sourced in bash.rc earlier
# args are "what is before the git-part" "what is after it" 'what to ask from git'
__git_ps1 "$PS1" "\[$txtreset\]\[$txtblue\]\[$txtbold\]${PWD#$git_top}" '(%s)'
else
# Not in git, let's use default green home-aware path
PS1+="\[$txtbold\]\[$txtgreen\]\w"
fi;
The whole script I've ended up writing:
set_prompt()
{
local last_cmd_return_code=$?
local txtreset='$(tput sgr0)'
local txtbold='$(tput bold)'
local txtblack='$(tput setaf 0)'
local txtred='$(tput setaf 1)'
local txtgreen='$(tput setaf 2)'
local txtyellow='$(tput setaf 3)'
local txtblue='$(tput setaf 4)'
local txtpurple='$(tput setaf 5)'
local txtcyan='$(tput setaf 6)'
local txtwhite='$(tput setaf 7)'
# escaping these so that it doesn't break multi-line prompts
# unicode "✗"
local fancyx='\342\[\234\227\]'
# unicode "✓"
local checkmark='\342\[\234\223\]'
# clearing ps1
PS1=""
# Part 1: a red "✗" or a green "✓" and the error number
if [[ $last_cmd_return_code == 0 ]]; then
PS1+="\[$txtgreen\]$checkmark\[$txtreset\] "
else
PS1+="\[$txtred\]$fancyx \[$txtreset\]$last_cmd_return_code "
fi
# Part 2: Add user
# default to bold
PS1+="\[$txtbold\]"
# red when we're root / id 0
if [[ $EUID == 0 ]]; then
PS1+="\[$txtred\]"
elif [[ $UID == 1000 ]]; then
# main user of the system gets purple
PS1+="\[$txtpurple\]"
else
# the rest are default and non-bold
PS1+="\[$txtreset\]"
fi
# Part 3: user@host
PS1+="\u\[$txtreset\]@\[$txtbold\]\[$txtcyan\]\h\[$txtreset\]:"
# Part 4: time to manipulate path, let's check if path is in git
local git_top=$(git rev-parse --show-toplevel 2>/dev/null)
if [ "$git_top" != "" ]; then
# we're in git
# substitute homedir
git_relative_top=${git_top/$HOME/\~}
reponame=${git_top##*/}
# green non-bold path to repo
PS1+="\[$txtreset\]\[$txtgreen\]${git_relative_top%$reponame}"
# repo itself is bold
PS1+="\[$txtbold\]$reponame\[$txtreset\]"
# now the branch info in blue
# apply the git ps1 sourced in bash.rc earlier
# args are "what is before the git-part" "what is after it" 'what to ask from git'
__git_ps1 "$PS1" "\[$txtreset\]\[$txtblue\]\[$txtbold\]${PWD#$git_top}" '(%s)'
else
# Not in git, let's use default green home-aware path
PS1+="\[$txtbold\]\[$txtgreen\]\w"
fi;
# Prompt, $ for user, # for root
if [[ $EUID == 0 ]]; then
PS1+="\[$txtreset\]\[$txtred\]"
else
PS1+="\[$txtreset\]\[$txtwhite\]"
fi
PS1+="\\$\[$txtreset\] "
# echo "$PS1"
}
PROMPT_COMMAND='set_prompt'
This ends up looking like so:
checkmark or cross and status for last command,
then user@hostname:~/path/to/project (branch)/path/in/repo
https://gist.github.com/justintv/168835?permalink_comment_id=3718502#gistcomment-3718502 I've been using this approach on all my servers, looks great, I'll provide an update at some point.
Heyy @javahaxxor
when i 'ls' my files and folders appear vertically. How do i make it look horizontally like you have in the above image??