Skip to content

Instantly share code, notes, and snippets.

@jftuga
Last active June 15, 2024 00:52
Show Gist options
  • Save jftuga/f3cd06f5f9f4ed5ec952e2ec8f42e07d to your computer and use it in GitHub Desktop.
Save jftuga/f3cd06f5f9f4ed5ec952e2ec8f42e07d to your computer and use it in GitHub Desktop.
My zsh prompt with current working directory, git branch name, and current time

Inspired by: Add Git Branch Name to Terminal Prompt

I have a two line zsh prompt:

  • line 1: current-directory [git branch]
  • line 2: hostname HH:MM:SS $

In my .zshrc file:

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}
COLOR_DEF=$'%f'
COLOR_DATE=$'%F{243}'
COLOR_DIR=$'%F{197}'
COLOR_GIT=$'%F{39}'
setopt PROMPT_SUBST
PROMPT_CWD='${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF}'
export NEWLINE=$'\n'
export PROMPT="${NEWLINE}${PROMPT_CWD}${NEWLINE}${COLOR_DATE}%m %D{%H:%M:%S}${COLOR_DEF} $ "

Here is what it looks like, with the directory name in red, the branch in blue, and the date in faded gray:

~/go/src/github.com/jftuga/dtdiff [dev]
mac 17:35:28 $ 

What I like about having the time:

  • If I meant to time something, but forgot to, I have a built-in timer of sorts
  • I can easily scroll back to see how long ago I ran a command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment