Skip to content

Instantly share code, notes, and snippets.

@jswright61
Created June 27, 2025 19:39
Show Gist options
  • Select an option

  • Save jswright61/69a039e122fb1f184e04f58b460176ab to your computer and use it in GitHub Desktop.

Select an option

Save jswright61/69a039e122fb1f184e04f58b460176ab to your computer and use it in GitHub Desktop.
Zsh prompt from Claude

create a zsh prompt command containing the following items in order.
The color of each item is listed before the item in parens. (blue) user name (purple) host name
(yellow) current working directory
(black) active git branch
(green) checkmark if my git repository is clean
(red) XXX if my git repository is dirty
(red) current Ruby version number

please add a right prompt on line 2 that displays the elapsed time in seconds of the last executed command

the code '$(date +%s%0N)/1000000)' throws an exception

Claude generated what, to my eyes, was pretty decent code at every turn. When I tried to implement the prompt, my Mac, using Terminal.app threw an exception because the nanoseconds are not supported, when I made the additional prompt it was corrected.

A pretty good result, I'd say.

# Add this to your ~/.zshrc file
# Enable prompt substitution
setopt PROMPT_SUBST
# Function to get git branch
git_branch() {
git branch 2>/dev/null | grep '^*' | sed 's/* //'
}
# Function to check git status
git_status() {
if git rev-parse --git-dir > /dev/null 2>&1; then
if [[ -z $(git status --porcelain) ]]; then
echo "%F{green}✓%f"
else
echo "%F{red}XXX%f"
fi
fi
}
# Function to get Ruby version
ruby_version() {
if command -v ruby > /dev/null 2>&1; then
ruby -v | awk '{print $2}'
fi
}
# Function to calculate command execution time
preexec() {
timer=$(date +%s)
}
precmd() {
if [ $timer ]; then
now=$(date +%s)
elapsed=$(($now-$timer))
unset timer
fi
}
# Set the prompt
PROMPT='%F{blue}%n%f@%F{magenta}%m%f:%F{yellow}%~%f%F{black}$(git_branch)%f$(git_status) %F{red}$(ruby_version)%f
$ '
# Set the right prompt with elapsed time
RPROMPT='%F{cyan}${elapsed:+${elapsed}s}%f'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment