set_PS1()
{
local RESET=$(tput sgr0 )
local BOLD=$(tput bold )
local RED=$(tput setaf 1 )
local GREEN=$(tput setaf 2 )
local YELLOW=$(tput setaf 3 )
local BLUE=$(tput setaf 4 )
local MAGENTABG=$(tput setab 5 )
local CYAN=$(tput setaf 6 )
local WHOAMI='\u'
local WHERE='\w'
local HOSTNAME='\h'
local DATE='\D{%Y-%m-%d %H:%M:%S}'
local LAST_RET='${?#0}'
local LINE_1="$YELLOW$DATE $GREEN$WHOAMI$RED@$CYAN$HOSTNAME $BLUE$BOLD$WHERE$RESET"
local LINE_2="\\[$MAGENTABG\\]$LAST_RET\\[$RESET\\]"'\$ '
PS1="$LINE_1\n$LINE_2"
unset -f set_PS1
}
set_PS1
sauce: https://codereview.stackexchange.com/q/174019Bash PS1 Generator
141–146 of 146 posts
Re: Bash PS1 Generator
#142Seeing '>' as an option makes it somewhat relevant to mention here: I'd recommend not ending your prompt with a '>' character (as you'd see in DOS/Windows a lot, which might make it seem like a nice option). If you accidentally copy and paste your prompt with a command after it, (which is easy to do with a couple mouse clicks, or if you forget what's in your clipboard after copying some lines from your terminal,) the…
Maybe >(Unicode 0xFF1/>) would be a good substitute? Side by side: >>
Here’s a few the IME on my phone suggested (the middle ones are the same as yours as far as I can tell):
〉> > ≫ 》
Re: Bash PS1 Generator
#143 function __col1_ps1 {
[[ $MC_SID ]] && return
local termios cur_y
# ask the terminal for any pending (line buffered) input
termios=$(stty --save) && stty -icanon && stty "$termios"
# if there's pending input, assume it's been echoed and we're not in first column
# otherwise ask the terminal for current column and read it from input
if read -t 0 || { IFS='[;' read -s -r -d'R' -p$'\033[6n' _ _ cur_y && [[ $cur_y != 1 ]]; }; then
echo $'\001\033[41m↵\033[m\002\n\001\r\002'
fi
}
PS1='$(__col1_ps1)'
My full prompt is here: https://github.com/liskin/dotfiles/blob/9785740f7f97d7da1f84...Re: Bash PS1 Generator
#144Earlier quoted context omitted.
In case someone wonders, this does retain the repeated "+" to indicate call depth: "The first character of PS4 is replicated multiple times, as necessary, to indicate multiple levels of indirection." -- this is an important feature when working on large-ish (1-10+k LoC) scripts, which end up having very deep call stacks and a flat trace output would be completely inscrutable.
If you have 10k LoC Bash scripts, then you’ve got bigger problems!
Re: Bash PS1 Generator
#145Notes: * As an idea, this is excellent. It's intuitive to use and provides full instructions on how to implement * Putting an element in any other position than the end of the prompt is fiddly. * I'd love for the option to to be able to colour elements. * More advanced or exotic element types such as Git Branch would be excellent (Not sure if Bash supports this, but I've seen it in some shells)
There's one prompt in ZSH, the adam2 prompt[1], that I completely fell in love with due to the automatic horizontal rule since it makes tracing histories so much easier when dealing with long-scrolling outputs. I've never been able to find a similar feature in another prompt/shell, nor have I had the inclination to try and replicate it, but I'd be much more inclined to switch shells/try new prompts if that were avail…
Re: Bash PS1 Generator
#146Earlier quoted context omitted.
This. The one time a month when I can't remember which git branch I'm on I'll just run 'git branch'. If you're feeling frisky, you could even alias that to 'gb'.
One time a month? Currently I have 85 work related repositories checked out, putting the current git branch and current Kubernetes context in the prompt is helping to avoid mistakes. I also have a newline in my prompt which makes it easier to copy commands and output into Jira.