Skip to content

Instantly share code, notes, and snippets.

@Veejay
Created September 11, 2010 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Veejay/575029 to your computer and use it in GitHub Desktop.
Save Veejay/575029 to your computer and use it in GitHub Desktop.
#!/bin/zsh
# vim:fdm=marker
#
# .zshrc
#
# by Sylvain Lebresne <lebresne@gmail.com>
#
# Prompt {{{1
autoload -U colors && colors
autoload -Uz vcs_info
setopt prompt_subst
# The eight basic colors are:
# black, red, green, yellow, blue, magenta, cyan, and white.
# The associative arrays are:
# $fg, $fg_bold, $bg, $bg_bold
# Moreveover, one can use $reset_color and $bold_color
#print "$fg[black]light black, $fg[red]light red, $fg[green]ligth green, $fg[yellow]light yellow, $fg[blue]light blue, $fg[magenta]light magenta, $fg[cyan]light cyan, $fg[white]light white" >> $TTY
#print "$fg_bold[black] bold black, $fg_bold[red] bold red, $fg_bold[green] bold green, $fg_bold[yellow] bold yellow, $fg_bold[blue] bold blue, $fg_bold[magenta] bold magenta, $fg_bold[cyan] bold cyan, $fg_bold[white] bold white" >> $TTY
# set some colors
for COLOR in BLACK RED GREEN YELLOW WHITE CYAN BLUE MAGENTA; do
eval PR_$COLOR='%{$fg[${(L)COLOR}]%}'
eval PR_BRIGHT_$COLOR='%{$fg_bold[${(L)COLOR}]%}'
done
PR_RESET="%{${reset_color}%}";
# set formats
# %b - branchname
# %u - unstagedstr (see below)
# %c - stangedstr (see below)
# %a - action (e.g. rebase-i)
# %R - repository path
# %S - path in the repository
FMT_BRANCH="[${PR_BRIGHT_BLACK}%b%u${PR_RESET}]" # e.g. master
FMT_ACTION="(${PR_CYAN}%a${PR_RESET}%)" # e.g. (rebase-i)
FMT_PATH="%R${PR_YELLOW}/%S" # e.g. ~/repo/subdir
# check-for-changes can be really slow.
# you should disable it, if you work with large repositories
zstyle ':vcs_info:*:prompt:*' disable bzr svk svn
zstyle ':vcs_info:*:prompt:*' check-for-changes false
zstyle ':vcs_info:*:prompt:*' unstagedstr '¹' # display ¹ if there are unstaged changes
zstyle ':vcs_info:*:prompt:*' stagedstr '²' # display ² if there are staged changes
zstyle ':vcs_info:*:prompt:*' actionformats "${FMT_BRANCH}${FMT_ACTION}" "${FMT_PATH}"
zstyle ':vcs_info:*:prompt:*' formats "${FMT_BRANCH}" "${FMT_PATH}"
zstyle ':vcs_info:*:prompt:*' nvcsformats "" "%~"
zstyle ':completion:*' special-dirs true
# stuff done before the prompt is drawn
function precmd {
vcs_info 'prompt'
# display user@host and full dir in *term title
print -Pn "\033]0;%n@%m %~\007"
}
function preexec () {
# display user@host and name of current process in *term title
print -Pn "\033]0;%n@%m <$1> %~\007"
}
function lprompt {
local color1=$1
local color2=$2
local color3=$3
local username="${color1}%n"
local at="${color1}@"
local hostname="${color1}%m${PR_RESET}"
local wd="${color2}%~${PR_RESET}"
local promptsign="${color3}>"
PROMPT="${PR_RESET}${username}${at}${hostname} (${wd}) ${promptsign} ${PR_RESET}"
}
function rprompt {
local color1=$1
local color2=$2
local date="${color2}%*${PR_RESET}"
local git_info='${${vcs_info_msg_0_%%.}}'
RPROMPT="${git_info}[${date}]"
}
lprompt $PR_BRIGHT_YELLOW $PR_BRIGHT_BLUE $PR_BRIGHT_BLACK
rprompt $PR_RESET $PR_BRIGHT_BLACK
# End Prompt 1}}}
# Shell options {{{1
# General goodness
setopt nobeep noclobber extended_glob transient_rprompt
setopt no_auto_menu
#setopt rm_star_silent
# XXX Experimental
#setopt auto_pushd pushd_ignore_dups
#setopt list_packed
#setopt hist_ignore_space
# History related settings
HISTFILE=~/.zsh_history
HISTSIZE=5000
SAVEHIST=5000
setopt append_history hist_save_no_dups hist_ignore_dups hist_no_store
setopt inc_append_history extended_history
# XXX Experimental. This should import the lines of history incrementally
# saved by other shells. This way we can avoid having out history polluted
# by other windows unless we want their lines.
alias histimport='fc -RI'
# This is handy, and needed for below
setopt equals
# I can't stand not having the stat command. Unfortunatly, most UNIXs
# don't have it. Zsh to the rescue! This module adds it as a builtin.
zmodload zsh/stat &> /dev/null && alias stat='stat -Lors'
# This is a nice idea. Hope I can train myself to use it
autoload -U zargs
# Always wanted something like vared. This gives us it.
autoload -U zed
# End shell options 1}}}
# Apps Specific conf {{{1
# grep
export GREP_OPTIONS="--color=auto"
export GREP_COLOR="1;34"
#export LANG="en_US.utf8"
# ack
#alias ack="ack-grep"
export ACK_COLOR_MATCH=blue
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(lesspipe)"
# colors for ls
[ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
[ -e "$DIR_COLORS" ] || DIR_COLORS=""
eval "`dircolors -b $DIR_COLORS`"
alias ls='ls --color=auto -F'
# End apps Specific conf 1}}}
# zstyle settings {{{1
# Completion function debugging settings. Comment out for normal use
#zstyle ':completion:*' verbose yes
#zstyle ':completion:*:descriptions' format %B%d%b
#zstyle ':completion:*:messages' format %d
#zstyle ':completion:*:warnings' format 'No matches for: %d'
#zstyle ':completion:*' group-name ''
#zstyle ':completion:*:open:*' use-cache yes
#zstyle ':completion:*:fink:*' use-cache yes
# Customize the completion mechanism:
# Make the kill command completion show me a list all the time so I can be
# sure I'm killing the right command. This works even with 'kill
# netscape<TAB>'
zstyle ':completion:*:*:kill:*' menu yes select
zstyle ':completion:*:kill:*' force-list always
# Make the ps list nicer
# TODO: This probably won't work on a BSD or MacOS yet
if (( $UID == 0 ))
then
# We are root, so let's see all processes
zstyle ':completion:*:complete:kill:*' command 'ps -e -o pid,user,time,tty,args'
else
# Not root. Let's just see our own
zstyle ':completion:*:complete:kill:*' command 'ps -u '$UID' -o pid,time,tty,args'
fi
#make expansion case insensitive
# (lower to upper)
#zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'
# (lower to upper AND upper to lower)
#zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
## case-insensitive,partial-word and then substring completion
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
#zstyle ':completion:*' list-colors ''
#zstyle ':completion:*' menu select=2
#zstyle ':completion:*' select-prompt %SScrolling active: current selection at %p%s
#zstyle :compinstall filename '/.zshrc'
autoload -U compinit
compinit
# complete as much u can ..
zstyle ':completion:*' completer _complete _list _oldlist _expand _ignored _match _correct _approximate _prefix
# complete less
#zstyle ':completion:*' completer _expand _complete _list _ignored _approximate
# complete minimal
#zstyle ':completion:*' completer _complete _ignored
# allow one error for every three characters typed in approximate completer
zstyle -e ':completion:*:approximate:*' max-errors 'reply=( $(( ($#PREFIX+$#SUFFIX)/3 )) numeric )'
# formatting and messages
zstyle ':completion:*' verbose yes
zstyle ':completion:*:descriptions' format $'%{\e[0;31m%}%d%{\e[0m%}'
zstyle ':completion:*:messages' format $'%{\e[0;31m%}%d%{\e[0m%}'
zstyle ':completion:*:warnings' format $'%{\e[0;31m%}No matches for: %d%{\e[0m%}'
zstyle ':completion:*:corrections' format $'%{\e[0;31m%}%d (errors: %e)%{\e[0m%}'
zstyle ':completion:*' group-name ''
# determine in which order the names (files) should be
# listed and completed when using menu completion.
# `size' to sort them by the size of the file
# `links' to sort them by the number of links to the file
# `modification' or `time' or `date' to sort them by the last modification time
# `access' to sort them by the last access time
# `inode' or `change' to sort them by the last inode change time
# `reverse' to sort in decreasing order
# If the style is set to any other value, or is unset, files will be
# sorted alphabetically by name.
zstyle ':completion:*' file-sort name
# how many completions switch on menu selection
# use 'long' to start menu compl. if list is bigger than screen
# or some number to start menu compl. if list has that number
# of completions (or more).
zstyle ':completion:*' menu select=long
# offer indexes before parameters in subscripts
zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
# insert all expansions for expand completer
zstyle ':completion:*:expand:*' tag-order all-expansions
# ignore completion functions (until the _ignored completer)
zstyle ':completion:*:functions' ignored-patterns '_*'
# completion caching
zstyle ':completion::complete:*' use-cache 1
zstyle ':completion::complete:*' cache-path ~/.zcompcache/$HOST
# add colors to completions
#zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' list-colors "di=34"
# don't complete backup files as executables
zstyle ':completion:*:complete:-command-::commands' ignored-patterns '*\~'
# filename suffixes to ignore during completion (except after rm command)
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns \
'*?.(o|c~|old|pro|zwc)' '*~'
# generic completions for programs which understand GNU long options(--help)
compdef _gnu_generic slrnpull make df du
## add colors to processes for kill completion
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31'
# hostnames and login
hosts=( $(</etc/hosts) 'idea.nguyen.vg')
zstyle ':completion:*' hosts $hosts
## (user,host) pairs
## all my accounts:
#my_accounts=(
# {joe,root}@mymachine.com
# jbloggs@myothermachine.com
#)
## other people's accounts:
#other_accounts=(
# {fred,root}@hismachine.com
# vera@hermachine.com
#)
#zstyle ':completion:*:my-accounts' users-hosts $my_accounts
#zstyle ':completion:*:other-accounts' users-hosts $other_accounts
## (host, port, user) triples for telnet
#telnet_users_hosts_ports=(
# user1@host1:
# user2@host2:
# @mail-server:{smtp,pop3}
# @news-server:nntp
# @proxy-server:8000
#)
#zstyle ':completion:*:*:telnet:*' users-hosts-ports $telnet_users_hosts_ports
# End zstyle settings 1}}}
# Key bindings {{{1
# Get things that way my fingers are pre-programmed
#bindkey -e
# Fix ^U
#bindkey "\C-U" backward-kill-line
# Love this in bash, lets get it here
#setopt interactive_comments
#bindkey "\M-#" pound-insert
#bindkey "\e#" pound-insert # not sure why zsh treats these differently
# XXX Experimental. This causes space to expand history events
#bindkey " " magic-space
# XXX Experimental. Hope I can remember to use this
#bindkey "\eq" push-input
# Make the menuselection turned on above even more interesting
# Needs this, which would have been loaded when we performed our first
# completion anyway
zmodload -i zsh/complist
bindkey -M menuselect '^o' accept-and-infer-next-history
# End key bindings 1}}}
# Functions {{{1
# sethostcolors {{{2
# Uses the ability of modern xterms to allow the colors to be set
# dynamically from the client app. Try to choose a base background color
# for each machine, then use slight variants on that color for the
# background
# TODO: make this integrate with the prompt color better to ensure
# readability of the prompt.
sethostcolors () {
# Get a word to base the color choice on.
local basename=${1:-$HOST}
# Get the red, green, and blue components from the basename in some semi-reproducible fashion
local hostnum=${${$(echo $basename | cksum )[0]}[2,4]}
local red=${hostnum[1]}
local green=${hostnum[2]}
local blue=${hostnum[3]}
local redcomp=$(( red * 8 + 180 ))
local greencomp=$(( green * 8 + 180 ))
local bluecomp=$(( blue * 8 + 180 ))
# Randomise them a bit
local randomfactor=8
redcomp=$(( redcomp + ( ( RANDOM * 2 * randomfactor / 32768 ) - randomfactor ) ))
greencomp=$(( greencomp + ( ( RANDOM * 2 * randomfactor / 32768 ) - randomfactor ) ))
bluecomp=$(( bluecomp + ( ( RANDOM * 2 * randomfactor / 32768 ) - randomfactor ) ))
if (( redcomp < 0 )); then redcomp=0; fi
if (( greencomp < 0 )); then greencomp=0; fi
if (( bluecomp < 0 )); then bluecomp=0; fi
if (( redcomp > 255 )); then redcomp=255; fi
if (( greencomp > 255 )); then greencomp=255; fi
if (( bluecomp > 255 )); then bluecomp=255; fi
# Make sure we can read it!
local fgcol
if (( redcomp + 0.5 + greencomp * 0.86 + bluecomp * 0.18 < 150 ))
then
# the background is quite dark. Make the text white
fgcol='#fff'
else
# make the text black
fgcol='#000'
fi
redcomp=${(l:2::0:)$(( [##16] redcomp ))}
greencomp=${(l:2::0:)$(( [##16] greencomp ))}
bluecomp=${(l:2::0:)$(( [##16] bluecomp ))}
local bgcol="rgb:$redcomp/$greencomp/$bluecomp"
print -n "\e]10;$fgcol;$bgcol\a"
}
# And run it if it makes sense
if [[ $TERM == *xterm ]]
then
sethostcolors
fi
# End sethostcolors 2}}}
# settitle/unsettitle {{{2
# Sets/unsets the title to be used for this window
settitle () {
windowtitle="$*"
icontitle="$*"
}
unsettitle () {
unset windowtitle
unset icontitle
}
# End settitle/unsettitle 2}}}
# histgrep {{{2
# Search for matching lines in the shell history.
# Params:
# Pattern: Shell pattern to search for. If it contains any * then it's
# used as is. Otherwise it's assumed to be a string that can
# appear anwhere in the line
# Start (optional): Which history even to start looking from. Passed
# straight to fc, so it can be an event #, negative to count
# back, or a string to search
# Stop (optional): Which event to stop looking for matched. Values are
# as for Start.
# TODO: Have something better than just "Is there a * in the pattern"
histgrep () {
[[ $# < 1 ]] && { print -u2 "No pattern supplied"; return 1 }
local pat="$1" start="${2:-1}" stop="${3:--1}"
[[ $pat = *\** ]] || pat="*$pat*"
fc -l -m "$pat" "$start" "$stop"
}
alias histgrep='noglob histgrep'
alias ack='/usr/bin/ack-grep'
# End histgrep 2}}}
# End functions 1}}}
# Load configuration specific to the machine {{{1
[ -f $HOME/.zshrc.local ] && . $HOME/.zshrc.local pre
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment