Shell Prompt Generator
Build a bash or zsh prompt and copy it in one block
Turn on the parts you want, user name, host, working directory, git branch, exit code of the last command, Python virtualenv, then pick a color for each one and watch the prompt render as you go. The result is a block you can paste straight into ~/.bashrc or ~/.zshrc. Color codes are marked as non-printing so a long command line does not wrap in the wrong place, the git branch disappears when you are outside a repository, and the exit code is captured before the prompt can overwrite it. Everything runs in your browser and there is no sign-in.
# BootSignal prompt, add this to ~/.bashrc
bs_git_branch() {
local b=$(git branch --show-current 2>/dev/null)
[ -n "$b" ] && printf ' (%s)' "$b"
}
PS1='\[\e[1;32m\]\u\[\e[0m\]@\[\e[1;32m\]\h\[\e[0m\]:\[\e[1;34m\]\w\[\e[0m\]\[\e[1;35m\]$(bs_git_branch)\[\e[0m\] \[\e[1m\]\$\[\e[0m\] 'Everything is assembled in your browser, nothing is uploaded. Paste the block into your shell first to try it, then add it to your config file when you are happy with it.
Questions
- Where do I put this?
- Append the block to ~/.bashrc (bash) or ~/.zshrc (zsh), then run source ~/.bashrc or open a new terminal. On macOS, bash often reads ~/.bash_profile instead.
- Why are the color codes wrapped in \[ \]?
- Bash counts every character in PS1 when it decides where the cursor is. Color codes print nothing, so they have to be marked as non-printing, otherwise a long command line wraps in the wrong place and starts overwriting your prompt. Zsh uses %F and %f instead, which it already treats as zero width.
- Why is the git branch a function?
- PS1 cannot run a conditional by itself, so the helper prints nothing at all when you are outside a repository. That keeps empty parentheses and stray separators out of the prompt, which is why no separator is placed next to the branch.
- Why does the exit code need PROMPT_COMMAND?
- In bash, any command substitution in the prompt runs before you read $?, so the status you see is the status of that substitution. Capturing it once in PROMPT_COMMAND keeps the real value. Zsh has the %(?..) conditional built in and needs no hook.