Skip to content

Instantly share code, notes, and snippets.

@JeanRibes
Created December 1, 2018 16:56
Show Gist options
  • Save JeanRibes/7193561843366a33dc90f919f88508df to your computer and use it in GitHub Desktop.
Save JeanRibes/7193561843366a33dc90f919f88508df to your computer and use it in GitHub Desktop.
create a stylish bash prompt with powerline-like arrows
#!/bin/bash
# Use this script to create a cool bash prompt: paste the output into your .bashrc
# look https://misc.flogisoft.com/bash/tip_colors_and_formatting#colors1 to get a list of available colors for 256-color terminals
# also, if you do not see the '>' character in your terminal, install the powerline fonts
PS1=""
lastbg=-1
function append {
PS1+="$1"
}
function color {
append "\[\e[0;38;5;$1;48;5;$2;1m\]"
}
function segment { # fg, bg, text
if [[ $lastbg -gt 0 ]]; then #do not display > symbol when beginning line
color $lastbg $2 #display '>' symbol with its last background as foreground so it appears on top of the new backgroud
append ""
fi
color $1 $2
append "$3"
lastbg=$2
}
#segment #FG BG TEXT
segment 226 17 " \u " #segment 46 17 "\u "
segment 255 6 ' @ '
segment 16 29 " \[\e[38;5;214m\]host\[\e[38;5;16m\].domain.tld" #segment 16 29 "\[\e[38;5;82m\]vps\[\e[38;5;16m\].ribes.ovh"
segment 252 240 " \w "
segment 0 0 " " #display a trailing '>'
append "\[\e[0m\]" #reset the prompt at the end of line
#echo $PS1 > ps1
echo "PS1='$PS1'"
echo "paste the preceding line in your .bashrc (you might want to setup a if statement because this works only in xterm-256color terminals"
echo
echo "here is a preview of the colors"
printf "$PS1"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment