public
Last active

My zsh prompt. No oh-my-zsh needed

  • Download Gist
prompt.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
## Find my full .zshrc at <github.com/mislav/dotfiles/blob/master/zshrc>.
 
# setup
 
autoload colors; colors;
export LSCOLORS="Gxfxcxdxbxegedabagacad"
setopt prompt_subst
 
# prompt
 
ZSH_THEME_GIT_PROMPT_PREFIX="%{$reset_color%}%{$fg[green]%}["
ZSH_THEME_GIT_PROMPT_SUFFIX="]%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}*%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_CLEAN=""
 
# show git branch/tag, or name-rev if on detached head
parse_git_branch() {
(command git symbolic-ref -q HEAD || command git name-rev --name-only --no-undefined --always HEAD) 2>/dev/null
}
 
# show red star if there are uncommitted changes
parse_git_dirty() {
if command git diff-index --quiet HEAD 2> /dev/null; then
echo "$ZSH_THEME_GIT_PROMPT_CLEAN"
else
echo "$ZSH_THEME_GIT_PROMPT_DIRTY"
fi
}
 
# if in a git repo, show dirty indicator + git branch
git_custom_status() {
local git_where="$(parse_git_branch)"
[ -n "$git_where" ] && echo "$(parse_git_dirty)$ZSH_THEME_GIT_PROMPT_PREFIX${git_where#(refs/heads/|tags/)}$ZSH_THEME_GIT_PROMPT_SUFFIX"
}
 
# show current rbenv version if different from rbenv global
rbenv_version_status() {
local ver=$(rbenv version-name)
[ "$(rbenv global)" != "$ver" ] && echo "[$ver]"
}
 
# put fancy stuff on the right
if which rbenv &> /dev/null; then
RPS1='$(git_custom_status)%{$fg[red]%}$(rbenv_version_status)%{$reset_color%} $EPS1'
else
RPS1='$(git_custom_status) $EPS1'
fi
 
# basic prompt on the left
PROMPT='%{$fg[cyan]%}%~% %(?.%{$fg[green]%}.%{$fg[red]%})%B$%b '

Wow! nice! Any chance we can reproduce this on bash?

Nvm, this was just the nudge I need to switch to zsh.

Hmm, can't quite reproduce this using vcs_info (see man zshcontrib), seems it considers untracked files to not be unstaged and it doesn't support tags. It does distinguish between staged and unstaged changes though.

autoload -Uz vcs_info

zstyle ':vcs_info:*:prompt:*'     check-for-changes true
zstyle ':vcs_info:*:prompt:*'     stagedstr         "%{$fg[green]%}*%{$reset_color%}"
zstyle ':vcs_info:*:prompt:*'     unstagedstr       "%{$fg[red]%}*%{$reset_color%}"
zstyle ':vcs_info:*:prompt:*'     branchformat      "%r"
zstyle ':vcs_info:*:prompt:*'     formats           "%u%c%{$fg[green]%}[%b]%{$reset_color%}"
zstyle ':vcs_info:*:prompt:*'     nvcsformats       ""

RPS1='$vcs_info_msg_0_'

[vcs_info]:

Ran into an issue with this where I needed to source from within a directory to get git info about it. That info would then follow me around regardless of what directory I was in.

Makes sense, but unworkable for me as a oh-my-zsh-free git prompt.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.