Skip to content

Instantly share code, notes, and snippets.

@mislav
Last active February 11, 2024 06:40
Show Gist options
  • Save mislav/1712320 to your computer and use it in GitHub Desktop.
Save mislav/1712320 to your computer and use it in GitHub Desktop.
My zsh prompt. No oh-my-zsh needed
## 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 '
@pcasaretto
Copy link

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

@pcasaretto
Copy link

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

@Nemo157
Copy link

Nemo157 commented Sep 6, 2012

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]:

@buley
Copy link

buley commented Jan 29, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment