Skip to content

Instantly share code, notes, and snippets.

@nmerouze
Created March 31, 2012 09:53
Show Gist options
  • Save nmerouze/2261381 to your computer and use it in GitHub Desktop.
Save nmerouze/2261381 to your computer and use it in GitHub Desktop.
# DESCRIPTION:
# A simple zsh configuration that gives you 90% of the useful features that I use everyday.
#
# AUTHOR:
# Geoffrey Grosenbach http://peepcode.com
############
# FUNCTIONS
############
# RVM or rbenv
function ruby_prompt(){
rv=$(rbenv version-name)
if (echo $rv &> /dev/null)
then
echo "%{$fg_bold[gray]%}ruby $rv%{$reset_color%}"
elif $(which rvm &> /dev/null)
then
echo "%{$fg_bold[gray]%}$(rvm tools identifier)%{$reset_color%}"
else
echo ""
fi
}
# Determine the time since last commit. If branch is clean,
# use a neutral color, otherwise colors will vary according to time.
function git_time_since_commit() {
if git rev-parse --git-dir > /dev/null 2>&1; then
# Only proceed if there is actually a commit.
if [[ $(git log 2>&1 > /dev/null | grep -c "^fatal: bad default revision") == 0 ]]; then
# Get the last commit.
last_commit=`git log --pretty=format:'%at' -1 2> /dev/null`
now=`date +%s`
seconds_since_last_commit=$((now-last_commit))
# Totals
MINUTES=$((seconds_since_last_commit / 60))
HOURS=$((seconds_since_last_commit/3600))
# Sub-hours and sub-minutes
DAYS=$((seconds_since_last_commit / 86400))
SUB_HOURS=$((HOURS % 24))
SUB_MINUTES=$((MINUTES % 60))
if [[ -n $(git status -s 2> /dev/null) ]]; then
if [ "$MINUTES" -gt 30 ]; then
COLOR="$ZSH_THEME_GIT_TIME_SINCE_COMMIT_LONG"
elif [ "$MINUTES" -gt 10 ]; then
COLOR="$ZSH_THEME_GIT_TIME_SHORT_COMMIT_MEDIUM"
else
COLOR="$ZSH_THEME_GIT_TIME_SINCE_COMMIT_SHORT"
fi
else
COLOR="$ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL"
fi
if [ "$HOURS" -gt 24 ]; then
echo "($COLOR${DAYS}d${SUB_HOURS}h${SUB_MINUTES}m%{$reset_color%}|"
elif [ "$MINUTES" -gt 60 ]; then
echo "($COLOR${HOURS}h${SUB_MINUTES}m%{$reset_color%}|"
else
echo "($COLOR${MINUTES}m%{$reset_color%}|"
fi
else
COLOR="$ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL"
echo "($COLOR~|"
fi
fi
}
function prompt_char {
git branch >/dev/null 2>/dev/null && echo '[±])' && return
hg root >/dev/null 2>/dev/null && echo '[☿])' && return
echo '[○]'
}
# Current path color depending on last command exit status
local current_path="%(?,%{$fg[gray]%}%~%{$reset_color%},%{$fg[red]%}%~%{$reset_color%})"
# mkdir & cd to it
function mcd() {
mkdir -p "$1" && cd "$1";
}
#########
# COLORS
#########
autoload -U colors
colors
setopt prompt_subst
ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg[white]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%})"
# Text to display if the branch is dirty
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%} *%{$reset_color%}"
# Text to display if the branch is clean
ZSH_THEME_GIT_PROMPT_CLEAN=""
# Colors vary depending on time lapsed.
ZSH_THEME_GIT_TIME_SINCE_COMMIT_SHORT="%{$fg[green]%}"
ZSH_THEME_GIT_TIME_SHORT_COMMIT_MEDIUM="%{$fg[yellow]%}"
ZSH_THEME_GIT_TIME_SINCE_COMMIT_LONG="%{$fg[red]%}"
ZSH_THEME_GIT_TIME_SINCE_COMMIT_NEUTRAL="%{$fg[cyan]%}"
#########
# PROMPT
#########
PROMPT='
${current_path}
%{$fg[white]%}$(git_time_since_commit)%{$fg[white]%}$(prompt_char) > %{$reset_color%}'
RPROMPT='%{$fg[white]%} $(ruby_prompt)$(~/bin/git-cwd-info.rb)%{$reset_color%}'
#############
# COMPLETION
#############
# Show completion on first TAB
setopt menucomplete
# load autocompletion
autoload -U compinit && compinit
# enable cache
zstyle ':completion:*' use-cache on
zstyle ':completion:*' cache-path ~/.zsh/cache
# ignore completion to commands we don't have
zstyle ':completion:*:functions' ignored-patterns '_*'
# format autocompletion style
zstyle ':completion:*:descriptions' format "%{$fg_bold[green]%}%d%{$reset_color%}"
zstyle ':completion:*:corrections' format "%{$fg_bold[yellow]%}%d%{$reset_color%}"
zstyle ':completion:*:messages' format "%{$fg_bold[red]%}%d%{$reset_color%}"
zstyle ':completion:*:warnings' format "%{$fg_bold[red]%}%d%{$reset_color%}"
# zstyle show completion menu if 2 or more items to select
zstyle ':completion:*' menu select=2
# zstyle kill menu
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:kill:*' force-list always
zstyle ':completion:*:*:kill:*:processes' list-colors "=(#b) #([0-9]#)*=36=31"
# enable color completion
zstyle ':completion:*:default' list-colors "=(#b) #([0-9]#)*=$color[yellow]=$color[red]"
# fuzzy matching of completions for when we mistype them
zstyle ':completion:*' completer _complete _match _approximate
zstyle ':completion:*:match:*' original only
# number of errors allowed by _approximate increase with the length of what we have typed so far
zstyle -e ':completion:*:approximate:*' max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'
##########
# ALIASES
##########
alias ls='ls -G'
alias ll='ls -lG'
alias duh='du -csh'
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
# Emits Git metadata for use in a Zsh prompt.
#
# AUTHOR:
# Ben Hoskings
# https://github.com/benhoskings/dot-files/blob/master/files/bin/git_cwd_info
#
# MODIFIED:
# Geoffrey Grosenbach http://peepcode.com
# The methods that get called more than once are memoized.
def git_repo_path
@git_repo_path ||= `git rev-parse --git-dir 2>/dev/null`.strip
end
def in_git_repo
!git_repo_path.empty? &&
git_repo_path != '~' &&
git_repo_path != "#{ENV['HOME']}/.git"
end
def git_parse_branch
@git_parse_branch ||= `git-current-branch`.chomp
end
def git_head_commit_id
`git rev-parse --short HEAD 2>/dev/null`.strip
end
def git_cwd_dirty
" %{\e[90m%}✗%{\e[0m%}" unless git_repo_path == '.' || `git ls-files -m`.strip.empty?
end
def rebasing_etc
if File.exists?(File.join(git_repo_path, 'BISECT_LOG'))
"+bisect"
elsif File.exists?(File.join(git_repo_path, 'MERGE_HEAD'))
"+merge"
elsif %w[rebase rebase-apply rebase-merge ../.dotest].any? {|d| File.exists?(File.join(git_repo_path, d)) }
"+rebase"
end
end
if in_git_repo
print " %{\e[90m%}#{git_parse_branch} %{\e[37m%}#{git_head_commit_id}%{\e[0m%}#{rebasing_etc}#{git_cwd_dirty}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment