# .zshrc # ZShell configuration # Do we want a mail count? MAILCOUNT=0 # Xdefaults dont work well all the time if [[ ! -f $HOME/.Xdefaults-$(uname -n) && -f $HOME/.Xdefaults ]] ; then ln -s $HOME/.Xdefaults $HOME/.Xdefaults-$(uname -n) fi # urxvt compensation if [[ "${TERM}" == "rxvt-unicode" ]] ; then export TERM=rxvt fi # Which implementation in shell function ewhich() { while [[ -n "$1" ]] ; do if type -p $1 &> /dev/null ; then echo $(type -p $1) else echo "" fi shift done } # Do we have which? if ! type -p which &> /dev/null ; then alias which="ewhich" ; fi # Updates the maildirs listing function get_maildirs() { local dirlist dirs dirlist=`echo $(ls $MAILDIR)` for dir in `echo ${IGNOREDIRS}` ; do dirlist="${dirlist/$dir /}" done for dir in `echo ${dirlist}` ; do if [[ -z "${dirs}" ]] ; then if [[ -d ${MAILDIR}/${dir}/new ]] ; then dirs="${dir}/new" fi else if [[ -d ${MAILDIR}/${dir}/new ]] ; then dirs="${dirs} ${dir}/new" fi fi done export MAILDIRS="${dirs}" } # Override the default variables export HOSTNAME="$(uname -n)" export HOSTTYPE="$(uname -m)" export COLORTERM=yes export CC=gcc export PAGER=/usr/bin/less export EDITOR=/usr/bin/vim if [[ ! $MAILCOUNT -eq 0 ]] ; then export MAILDIR=$HOME/.maildir/ export IGNOREDIRS="spam virus" get_maildirs fi # UTF-8 Sexiness!! export LANG="en_US.utf8" export LC_ALL="en_US.utf8" export LC="en_US.utf8" export LESSCHARSET="utf-8" # GNU Colors [ -f /etc/DIR_COLORS ] && eval $(dircolors -b /etc/DIR_COLORS) export ZLSCOLORS="${LS_COLORS}" # Alias commands alias ls="ls --color=auto -h" alias mv="nocorrect mv" alias cp="nocorrect cp" alias man="nocorrect man" alias mkdir="nocorrect mkdir" alias mkcd="nocorrect mkcd" alias rm="nocorrect rm -ir" alias ping="ping -c4" alias df="df -h" alias cgrep="grep --color=auto" alias :q="exit" alias :wq="exit" if type -p colorcvs &> /dev/null ; then alias cvs="colorcvs" ; fi if type -p colordiff &> /dev/null ; then alias diff="colordiff" ; fi if type -p colorgcc &> /dev/null ; then alias gcc="colorgcc" ; fi if type -p colortail &> /dev/null ; then alias tail="colortail" ; fi if ! type -p ftp &> /dev/null ; then alias ftp="zftp" ; fi # Bind keys bindkey "\e[H" beginning-of-line bindkey "\e[F" end-of-line #bindkey '^[[4~' end-of-line #bindkey '^[[1~' beginning-of-line bindkey "\e[2~" overwrite-mode bindkey "\e[3~" delete-char bindkey "\e[5~" up-line-or-history bindkey "\e[6~" down-line-or-history bindkey "\e" vi-cmd-mode bindkey -A viins main set -o vi # Load Modules autoload -U colors; colors autoload -U compinit; compinit autoload -U promptinit; promptinit zmodload zsh/complist zmodload zsh/termcap autoload -U zargs autoload -U zmv autoload -U zed autoload -U zcalc autoload -U zftp if [[ ${ZSH_VERSION//\./} -ge 420 ]] ; then autoload -U url-quote-magic zle -N self-insert url-quote-magic fi # Disable core dumps limit coredumpsize 0 # Configure ZSH History setopt HIST_REDUCE_BLANKS setopt HIST_IGNORE_DUPS setopt HIST_IGNORE_SPACE setopt INC_APPEND_HISTORY setopt SHARE_HISTORY setopt BANG_HIST HISTFILE=${HOME}/.zsh_history SAVEHIST=1000 HISTSIZE=1600 # New hashs hash -d sandbox=${HOME}/sandbox if [[ -d ${HOME}/work ]] ; then hash -d work=${HOME}/work for dir in $(ls ${HOME}/work) ; do hash -d ${dir}=${HOME}/work/${dir} done fi # Default File mask umask 022 # Terminal Options setopt AUTO_CD # implicate cd for non-commands setopt CD_ABLE_VARS # read vars in cd setopt CORRECT # correct spelling setopt COMPLETE_IN_WORD # complete commands anywhere in the word setopt NOTIFY # Notify when jobs finish setopt C_BASES # 0xFF setopt BASH_AUTO_LIST # Autolist options on repeition of ambiguous args setopt CHASE_LINKS # Follow links in cds setopt AUTO_PUSHD # Push dirs into history setopt ALWAYS_TO_END # Move to the end on complete completion setopt LIST_ROWS_FIRST # Row orientation for menu setopt MULTIOS # Allow Multiple pipes setopt MAGIC_EQUAL_SUBST # Expand inside equals setopt EXTENDED_GLOB if [[ "$MAILCOUNT" -eq "0" ]]; then PREFIX="" else PREFIX=$'%{\e[01;33m%}[%{\e[01;36m%}%1v%{\e[01;33m%}] ' fi if [[ "$(who -m | awk '{print $6}' | sed -e 's/(:.*//g')" = "" ]] ; then PS1=${PREFIX}$'%{\e[01;32m%}%n@%m %{\e[01;34m%}%1~ %# %{\e[0m%}' else PS1=${PREFIX}$'%{\e[01;36m%}%n %# %{\e[0m%}' RPROMPT=$'%{\e[01;33m%}%m %{\e[01;32m%}%1~%{\e[0m%}' fi # Mail Counter! function mailcount() { if [[ MAILCOUNT -eq 1 ]] ; then local mcount=0 for dir in `echo $MAILDIRS`; do mcount="$(expr $mcount + $(ls ${MAILDIR}$dir | wc -l))" done echo $mcount fi } # Terminal Title case $TERM in xterm*|rxvt*|(dt|k|E|a)term) precmd() { print -Pn "\e]2;%n@%m:%~\a" psvar[1]="$(mailcount)" } preexec() { print -Pn "\e]2;%n@%m:$1\a" } ;; screen) precmd() { print -Pn "\e\"%n@%m:%~\e\134" psvar[1]="$(mailcount)" } preexec() { print -Pn "\e\"%n@%n:$1\e\134" } ;; linux) ;; *) precmd() { psvar[1]="$(mailcount)" } ;; esac # We are lazy -- look up spaces too bindkey ' ' magic-space # Menu!! zstyle ':completion:*' menu select=1 # Colors on completion me-ow zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS} zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#)*=0=01;31' # Completion caching zstyle ':completion::complete:*' use-cache on zstyle ':completion::complete:*' cache-path ~/.zsh/cache/$HOST # Completion Options zstyle ':completion:*:match:*' original only zstyle ':completion::prefix-1:*' completer _complete zstyle ':completion:predict:*' completer _complete zstyle ':completion:incremental:*' completer _complete _correct zstyle ':completion:*' completer _complete _prefix _correct _prefix _match _approximate # Ignore completions for commands that we dont have zstyle ':completion:*:functions' ignored-patterns '_*' # Dont complete backups as executables zstyle ':completion:*:complete:-command-::commands' ignored-patterns '*\~' # Path Expansion zstyle ':completion:*' expand 'yes' zstyle ':completion:*' squeeze-shlashes 'yes' zstyle ':completion::complete:*' '\\' # Allow forced showing' zstyle '*' single-ignored show # Case insensitivity, partial matching, substitution zstyle ':completion:*' matcher-list 'm:{A-Z}={a-z}' 'm:{a-z}={A-Z}' 'r:|[._-]=** r:|=**' 'l:|=* r:|=*' # Group matches and Describe zstyle ':completion:*:matches' group 'yes' zstyle ':completion:*:options' description 'yes' zstyle ':completion:*:options' auto-description '%d' zstyle ':completion:*:descriptions' format $'\e[01;33m -- %d --\e[0m' zstyle ':completion:*:messages' format $'\e[01;35m -- %d --\e[0m' zstyle ':completion:*:warnings' format $'\e[01;31m -- No Matches Found --\e[0m' # Prevent re-suggestion zstyle ':completion:*:rm:*' ignore-line yes zstyle ':completion:*:scp:*' ignore-line yes zstyle ':completion:*:ls:*' ignore-line yes # Menu for KILL zstyle ':completion:*:*:kill:*' menu yes select zstyle ':completion:*:kill:*' force-list always # Kill Menu Extension! zstyle ':completion:*:processes' command 'ps -U $(whoami) | sed "/ps/d"' zstyle ':completion:*:processes' insert-ids menu yes select # Remove uninteresting users zstyle ':completion:*:*:*:users' ignored-patterns \ adm alias apache at bin cron cyrus daemon ftp games gdm guest \ haldaemon halt mail man messagebus mysql named news nobody nut \ lp operator portage postfix postgres postmaster qmaild qmaill \ qmailp qmailq qmailr qmails shutdown smmsp squid sshd sync \ uucp vpopmail xfs # Remove uninteresting hosts zstyle ':completion:*:*:*:hosts-host' ignored-patterns \ '*.*' loopback localhost zstyle ':completion:*:*:*:hosts-domain' ignored-patterns \ '<->.<->.<->.<->' '^*.*' '*@*' zstyle ':completion:*:*:*:hosts-ipaddr' ignored-patterns \ '^<->.<->.<->.<->' '127.0.0.<->' zstyle -e ':completion:*:(ssh|scp):*' hosts 'reply=( ${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) \ /dev/null)"}%%[# ]*}//,/ } )' # SSH Completion zstyle ':completion:*:scp:*' tag-order \ files users 'hosts:-host hosts:-domain:domain hosts:-ipaddr"IP\ Address *' zstyle ':completion:*:scp:*' group-order \ files all-files users hosts-domain hosts-host hosts-ipaddr zstyle ':completion:*:ssh:*' tag-order \ users 'hosts:-host hosts:-domain:domain hosts:-ipaddr"IP\ Address *' zstyle ':completion:*:ssh:*' group-order \ hosts-domain hosts-host users hosts-ipaddr # Make Completion compile=(all clean compile disclean install remove uninstall) compctl -k compile make # Command File Type Detection compctl -g '*.ebuild' ebuild compctl -g '*.tex' + -g '*(-/)' latex compctl -g '*.dvi' + -g '*(-/)' dvipdf dvipdfm compctl -g '*.java' + -g '*(-/)' javac compctl -g '*.tar.bz2 *.tar.gz *.bz2 *.gz *.jar *.rar *.tar *.tbz2 *.tgz *.zip *.Z' + -g '*(-/)' extract compctl -g '*.mp3 *.ogg *.mod *.wav *.avi *.mpg *.mpeg *.wmv' + -g '*(-/)' mplayer compctl -g '*.py' python compctl -g '*(-/D)' cd compctl -g '*(-/)' mkdir # Command Parameter Completion compctl -z fg compctl -j kill compctl -j disown compctl -u chown compctl -u su compctl -c sudo compctl -c which compctl -c type compctl -c hash compctl -c unhash compctl -o setopt compctl -o unsetopt compctl -a alias compctl -a unalias compctl -A shift compctl -v export compctl -v unset compctl -v echo compctl -b bindkey # Handle logouts trap exit_handler 0 function exit_handler() { # If we have a symlink to Xdefaults .. destroy it if [[ -h $HOME/.Xdefaults-$(uname -n) ]] ; then rm -f "$HOME/.Xdefaults-$(uname -n)" fi clear } # Welcome message for login shells if [[ $SHLVL -eq 1 ]] ; then echo print -P "\e[1;32m Welcome to: \e[1;34m%m" print -P "\e[1;32m Running: \e[1;34m`uname -srm`\e[1;32m on \e[1;34m%l" print -P "\e[1;32m It is:\e[1;34m %D{%r} \e[1;32m on \e[1;34m%D{%A %b %f %G}" echo fi # Custom commands extract() { if [[ -z "$1" ]] ; then print -P "usage: \e[1;36mextract\e[1;0m < filename >" print -P " Extract the file specified based on the extension" elif [[ -f $1 ]] ; then case ${(L)1} in *.tar.bz2) tar -jxvf $1 ;; *.tar.gz) tar -zxvf $1 ;; *.bz2) bunzip2 $1 ;; *.gz) gunzip $1 ;; *.jar) unzip $1 ;; *.rar) unrar x $1 ;; *.tar) tar -xvf $1 ;; *.tbz2) tar -jxvf $1 ;; *.tgz) tar -zxvf $1 ;; *.zip) unzip $1 ;; *.Z) uncompress $1 ;; *) echo "Unable to extract '$1' :: Unknown extension" esac else echo "File ('$1') does not exist!" fi } calendar() { if [[ ! -f /usr/bin/cal ]] ; then echo "Please install cal before trying to use it!" return fi if [[ "$#" = "0" ]] ; then /usr/bin/cal | egrep -C 40 --color "\<$(date +%e| tr -d ' ')\>" else /usr/bin/cal $@ | egrep -C 40 --color "\<($(date +%B)|$(date +%e | tr -d ' '))\>" fi } alias cal=calendar basiccalc() { if [[ ! -f /usr/bin/bc ]] ; then echo "Please install bc before trying to use it!" return fi if [[ -z "$1" ]] ; then /usr/bin/bc -q else echo "$@" | /usr/bin/bc -l fi } alias bc=basiccalc echon() { # Convert to normal 0th element access local loc=$1 shift echo $@[(w)$loc] } mkcd() { if [[ -z "$1" ]] ; then echo "usage: \e[1;36mmkcd \e[1;0m< directory >" echo " Creates the specified directory and then changes it to pwd" else if [[ ! -d $1 ]] ; then mkdir -p $1 && cd $1 else cd $1 fi fi } unkey_host() { if [[ -z "$1" ]] ; then echo "usage: \e[1;36munkey_host \e[1;0m< host >" echo " Removes the specified host from ssh known host list" else sed -i -e "/$1/d" $HOME/.ssh/known_hosts fi } # Compatibility setenv() { typeset -x "${1}${1:+=}${(@)argv[2,$3]}" } # Begining of Gentoo Specific Functions!! # Gentoo Linux (http://www.gentoo.org) if [[ -f /etc/gentoo-release ]] ; then # Shamelessly stolen from ciaranm eportdir() { eval "eportdir() { echo $(portageq portdir) ; }" eportdir $* } eoverlays() { eval "eoverlays() { echo $(portageq portdir_overlay) ; }" eoverlays $* } # Shamelessly stolen from slarti # Modified to add in overlay support _useflags() { local global_use local_use overlay_global overlay_local global_use=${${${(M)${(f)"$(<$(eportdir)/profiles/use.desc)"}##* - *}/ - *}} local_use=${${${(M)${(f)"$(<$(eportdir)/profiles/use.local.desc)"}##* - *}/ - *}#*:} overlay_global="" overlay_local="" for overlay in $(eoverlays) ; do if [[ -r ${overlay}/profiles/use.desc ]] ; then overlay_global="${overlay_global} ${${(M)${(f)"$(<${overlay}/profiles/use.desc)"}##* - *}/ - *}" fi if [[ -r ${overlay}/profiles/use.local.desc ]] ; then overlay_local="${overlay_local} ${${${(M)${(f)"$(<${overlay}/profiles/use.local.desc)"}##* - *}/ - *}#*:}" fi done global_use="${global_use} ${overlay_global}" local_use="${local_use} ${overlay_local}" _tags global_use && { compadd "$@" ${(kv)=global_use} } _tags local_use && { compadd "$@" ${(kv)=local_use} } } describe() { if [[ -z "$1" ]] ; then echo "usage: \e[1;36mdescribe\e[1;0m < use flag > [ < use flag > ]" echo " Prints a description of the use flag" return fi for val in "$@" ; do local g_desc="$(sed -ne "s,^\([^ ]*:\)\?$val - ,,p" "${$(eportdir)}/profiles/use.desc" | uniq)" if [[ ! -n "$g_desc" ]] ; then l_desc="$(sed -ne "s,^\([^ ]*:\)\?$val - ,,p" "${$(eportdir)}/profiles/use.local.desc" | uniq)" fi if [[ -n "$g_desc" ]] ; then echo "\tThe \e[1;32mglobal\e[01;0m use flag '\e[1;32m$val\e[0;0m' is decribed as follows:" echo "\t$g_desc" elif [[ -n "$l_desc" ]] ; then echo "\tThe \e[1;32mlocal\e[01;0m use flag '\e[1;32m$val\e[0;0m' is decribed as follows:" echo "\t$l_desc" else echo "\tThe use flag '\e[1;31m$val\e[0;0m' was not found." fi done } syncdate() { if [[ ! -x /usr/bin/genlop ]] ; then echo "Please emerge genlop for this functionality" return fi date="$(genlop -nr | /usr/bin/tail -n1 | sed -e 's:.*>>> \(.*\)$:\1:')" echo "Portage was last synced: ${date}" } # Shamelessly stolen from ciaranm efind() { local efinddir cat pkg efinddir="$(eportdir)" case $1 in *-*/* ) pkg="${1##*/}" cat="${1%/*}" ;; ?*) pkg=${1} cat=$(echon 1 ${efinddir}/*-*/${pkg}/*.ebuild) [[ -f $cat ]] || cat=$(echon 1 ${efinddir}/*-*/${pkg}*/*.ebuild) [[ -f $cat ]] || cat=$(echon 1 ${efinddir}/*-*/*${pkg}/*.ebuild) [[ -f $cat ]] || cat=$(echon 1 ${efinddir}/*-*/*${pkg}*/*.ebuild) if [[ ! -f $cat ]] ; then return 1 fi pkg=${cat%/*} pkg=${pkg##*/} cat=${cat#${efinddir}/} cat=${cat%%/*} ;; esac echo "${cat}/${pkg}" } showkeywords() { local a c e f k l n p v p="$(eportdir)/profiles" for overlay in $(eoverlays) ; do if [[ -d ${overlay}/profiles ]] ; then p="${p} ${overlay}/profiles" fi done for profile in ${(s| |)p} ; do if [[ -r ${profile}/arch.list ]] ; then for arch in $(<${profile}/arch.list) ; do if [[ $a != *$arch* ]] ; then if [[ -z ${a} ]] ; then a="${arch}" else a="${a} ${arch}" fi fi done fi done a="$(echo $a | tr ' ' '\n' | sort -d | tr '\n' ' ')" for x in $(seq 1 9) ; do printf "%-25s| " "" for arch in ${(s| |)a} ; do printf "%1s%1s " "$arch[${x}]" "$arch[$((9 + ${x}))]" done echo "" done printf "%-25s" "--------------------------" for arch in ${(s| |)a} ; do printf "%3s" "---" done echo "" c="$(pwd)" p="$(eportdir) $(eoverlays)" while [[ -n "$1" ]] ; do f="$(efind $1)" for overlay in ${(s| |)p} ; do l="${overlay}/${f}" if [[ -d ${l} ]] ; then cd ${l} n=$(pwd) ; n=${n##*/} for e in *.ebuild ; do v=${e%%.ebuild} ; v=${v##${n}-} printf "%-25s| " ${v} for arch in ${(s| |)a} ; do k=$(cat "${e}" | sed -ne "s/^KEYWORDS=.*\([-~ \"]${arch}[^a-zA-Z0-9\-]\).*/\1/p") case ${k} in \~${arch}?) echo -ne "\e[01;33m~ " ;; \-${arch}?) echo -ne "\e[01;31m- " ;; ?${arch}?) echo -ne "\e[01;34m+ " ;; *) if grep "KEYWORDS=.*-\*" ${e} > /dev/null ; then echo -ne "\e[01;31e* " else echo -ne " " fi ;; esac done echo -e "\e[00m" done fi done shift done cd ${c} } compdef _useflags describe fi # Master Override!! if [[ -d ${HOME}/.zsh ]] ; then for file in $(ls ${HOME}/.zsh/); do . ${HOME}/.zsh/$file done fi