Utente:Buccia/zsh/zshrc

Da LinuxVar, il LUG della provincia di Varese.

I'm a huge supporter of zsh, which i think to be the definitive shell. I publish my zshrc to show (a little part of) what zsh can do, and to be able to retrieve it quickly when i'm a guest on someone's machine :)

See also: Utente:Buccia/zsh/zshenv

# buccias .zshrc
# vim: set ft=zsh tw=78 noet ts=2 sw=2 wrap:
 
# to use zsh on multiuser systems, and people dont like (read: dont know)
# zsh, you can use the following snippet in roots .bash_profile:
 
# if [ $(stat -c '%U' /proc/$(awk '/^PPid:/ {print $2}' < /proc/$$/status)) == fvezzosi ] ; then
#   exec zsh
# fi
 
 
# some functions.. need to be here
login_info () {
	# output useful info
	# NOTE: I dont use =pal and =xpath because it would output stuff to stderr
	if [ -x $(which pal 2>/dev/null) ] ; then
		print
		pal --color
 
		sort ~/.pal/uni-exams.pal | while read date text ; do
			[[ "${date}" -ge "$(date +%Y%m%d)" ]] &&
				print "\e[1;33m$(date -d ${date} +%d/%m)"\
					"\e[1;32m($(( ($(date -d ${date} +%s) - $(date +%s)) / 86400 )) d)"\
					"\e[1;31m${text}\e[0m"
			# prints out:
			# DD/MM (N days left) Text of the entry
		done
	fi
 
	if [ -x $(which xpath 2>/dev/null) -a -x $(which xsltproc 2>/dev/null) -a -r ~/.hnb ] ; then
		print
		# xpath -q -e '/tree[1]/node/data[text() = "daily '$(date '+%d/%m/%Y')'"]/../*' < ~/.hnb \
			# | cat
		if [ $(date +%s -d '2 days ago') -gt $(stat -c '%Y' ~/.hnb) ]; then
			print -f "\t\t\e[1;31m!! WARNING !!\n\
				\e[1;33myour .hnb file was not updated in the last two days\n\
				please update it to reflect the current situation\e[0m\n"
		fi
	fi
 
	zle && zle redisplay || return 0
}
# make it also a widget
zle -N login_info
 
# gets called every some seconds..
# check whether the day has changed, and if so, show the initial info
alarm_handler () {
	old_timestamp="${timestamp}"
	timestamp="$(date +%0H%0M%0S)"
 
	[ "${timestamp}" -ge "${old_timestamp}" -o $UID -eq 0 ] &&
		return
 
	clear
	login_info
	zle redisplay
}
 
# different behaviors for different machines
if [[ "${HOST}" != 'lothlorien' ]] ; then
	# when on ssh connection
	# put hostname before the path in screen title
	if [[ "${TERM}" == screen* ]] ; then
		if [ $(id -u) -eq '0' ] ; then
			preexec () { builtin echo -ne "\eksu $HOST:${PWD/$HOME/~}  ### [ $1 ]\e\\"; LASTCOMM="$1" }
			precmd () { builtin echo -ne "\eksu $HOST:${PWD/$HOME/~}  ### ( $LASTCOMM )\e\\" }
		else
			preexec () { builtin echo -ne "\ek$HOST:${PWD/$HOME/~} [ $1 ]\e\\"; LASTCOMM="$1" }
			precmd () { builtin echo -ne "\ekzsh $HOST:${PWD/$HOME/~} ( $LASTCOMM )\e\\" }
		fi
	else
		screen-Urx () { if [ -z "$STY$BUFFER" ]; then BUFFER="=screen -Urx"; zle accept-line; fi }
		zle -N screen-Urx
		bindkey "^[x" screen-Urx
	fi
 
else
	# this only happens on my laptop
	# funky colors in terminals
  # case "$(basename $(tty))" in
    # tty1) setterm -foreground green -store ;;
    # tty2) setterm -foreground cyan -store ;;
    # tty3) setterm -foreground red -store ;;
    # *) : ;;
  # esac
 
	# 1337 screen title setting
	if [[ "${TERM}" == screen* ]] ; then
		if [ $(id -u) -eq 0 ] ; then
			preexec () { builtin echo -ne "\eksu   ###   ${PWD/$HOME/~}  -  $1\e\\"; LASTCOMM="$1" }
			precmd () { builtin echo -ne "\eksu  ###   ${PWD/$HOME/~} ( $LASTCOMM )\e\\" }
		else
			preexec () { builtin echo -ne "\ek${PWD/$HOME/~} [ $1 ]\e\\"; LASTCOMM="$1" }
			precmd () { builtin echo -ne "\ekzsh ${PWD/$HOME/~} ( $LASTCOMM )\e\\" }
		fi
	fi
 
	[[ "${UID}" != 0 ]] && login_info
fi
 
# simple and cool prompt
PROMPT=$'%(0?::%{\e[1;41;33m%} %? %{\e[0m%} )%{\e[1;37m%}%(!:root :)%M%{\e[0m%}:%B%l%b %!%# '
 
# dumb programs need those
export COLUMNS
export LINES
 
export EDITOR=vim
export WWW_HOME='http://programming.reddit.com/'
 
# export LC_ALL="en_US.ISO-8859-15"
export LC_ALL="en_US.UTF-8"
export LANG="en_US.UTF-8"
 
# no comment
export CLASSPATH=~/.classpath:.
export JAVA_HOME=/usr/lib/jvm/java-6-sun-1.6.0.00/
 
# misc options
setopt autocd autolist extendedhistory
setopt histignoredups ignoreeof noclobber
setopt pushdminus dotglob
unsetopt automenu autoremoveslash
 
HISTSIZE=1024
LISTMAX=1024
unset MAILCHECK
 
# produce core files
ulimit -c unlimited
 
# colored filename/directory completion
# Attribute codes:
# 00 none  01 bold  04 underscore  05 blink  07 reverse  08 concealed
# Text color codes:
# 30 black  31 red  32 green  33 yellow  34 blue  35 magenta  36 cyan  37 white
# Background color codes:
# 40 black  41 red  42 green  43 yellow  44 blue  45 magenta  46 cyan  47 white
export LS_COLORS='no=0:fi=0:di=1;34:ln=1;36:pi=40;33:so=1;35:do=1;35:bd=40;33;1:cd=40;33;1:or=40;31;1:ex=1;32:*.tar=1;31:*.tgz=1;31:*.arj=1;31:*.taz=1;31:*.lzh=1;31:*.zip=1;31:*.rar=1;31:*.z=1;31:*.Z=1;31:*.gz=1;31:*.bz2=1;31:*.tbz2=1;31:*.deb=1;31:*.pdf=1;31:*.jpg=1;35:*.jpeg=1;35:*.gif=1;35:*.bmp=1;35:*.pbm=1;35:*.pgm=1;35:*.ppm=1;35:*.pnm=1;35:*.tga=1;35:*.xbm=1;35:*.xpm=1;35:*.tif=1;35:*.tiff=1;35:*.png=1;35:*.mpg=1;35:*.mpeg=1;35:*.mov=1;35:*.avi=1;35:*.wmv=1;35:*.ogg=1;35:*.mp3=1;35:*.mpc=1;35:*.wav=1;35:*.au=1;35:*.swp=1;30:*.pl=36:*.c=36:*.cc=36:*.h=36:*.core=1;33;41:*.gpg=1;33:'
export ZLS_COLORS="$LS_COLORS"
 
# black magic
zmodload -i zsh/complist
zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
# zstyle ':completion:*:default' list-prompt ''
 
alias :q='bye'
alias ls='ls -F --col'
alias -g D=DISPLAY=:0.0
alias feht='D feh -t --cache-thumbnails'
 
if [[ "${TERM}" == screen* && ! -z "${STY}" ]] ; then
	for cmd in aumix links mocp mutt man su top ; do
		# these only make sense inside a screen shell
		eval "alias ${cmd}='screen ${cmd}'"
	done
 
	alias run-help='screen man -a'
else
	alias screen='=screen -U'
fi
 
# i need $DISPLAY only for these
if [[ -z "${DISPLAY}" ]] ; then
	for cmd in feh xpdf ; do
		eval "alias ${cmd}='D ${cmd}'"
	done
fi
 
# word boundaries dont cross slashes (expunge the '/' character from the list
# of characters that are part of words)
WORDCHARS=${WORDCHARS//\//}
 
# act as i say!
bindkey -e
bindkey ^z run_fg_editor
bindkey ^x menu-select
bindkey ^i ls_if_emptybuf_or_expand
bindkey ^d ignoreeof_three_times
bindkey '^[l' login_info
bindkey '^[v' edit_command_line
bindkey '^[!' edit_command_output
 
# sort of digraphs
autoload insert-composed-char
zle -N insert-composed-char
bindkey ^k insert-composed-char
 
# insert file patterns on command line
autoload insert-files
zle -N insert-files
bindkey ^Xf insert-files
 
# completion
bindkey '^[i' menu-complete
bindkey '^[^i' reverse-menu-complete
 
# redraw the prompt every 59 seconds, so the time is always correct on the
# bottom-right side of the screen
timestamp=000000
TMOUT=59
trap "alarm_handler" ALRM
 
# my widgets
run_fg_editor () {
	# only makes sense when there is a %vi job
	jobs vi 2>/dev/null >/dev/null ||
		return
	[[ "${TERM}" == screen* ]] &&
# 		jobs | awk -f ~/c0de/awk_for_loop.awk
		preexec 'vim {fg}'
	fg %vi 2>/dev/null
	zle redisplay
}
zle -N run_fg_editor
 
# from zsh-users
edit_command_line () {
	# edit current line in $EDITOR
	local tmpfile=${TMPPREFIX:-/tmp/zsh}ecl$$
 
	print -R - "$PREBUFFER$BUFFER" >$tmpfile
	exec </dev/tty
	${VISUAL:-${EDITOR:-vi}} $tmpfile
	zle kill-buffer
	BUFFER=${"$(<$tmpfile)"/$PREBUFFER/}
	CURSOR=$#BUFFER
 
	command rm -f $tmpfile
	zle redisplay
}
zle -N edit_command_line
 
# ever used this :?
edit_command_output () {
	local output
	output=$(eval $BUFFER) || return
	BUFFER=$output
	CURSOR=0
}
zle -N edit_command_output
 
# .. pretty fly for a white guy
ls_if_emptybuf_or_expand () {
	if [ ! -z "${BUFFER}" ] ; then
		zle expand-or-complete-prefix
		if [[ "${BUFFER[-1]}" == / ]] ; then
			zle expand-or-complete-prefix
		fi
		return
	fi
 
	# grey magic .. almost black! :)
	BUFFER=". "
	CURSOR=2
	zle expand-or-complete-prefix
  # zle expand-or-complete-prefix
	BUFFER=
 
	# just a newline
	local IFS='
'
	# if there are less than two files..
	if [[ ${#$(=ls -A)} -eq 1 ]] ; then
		zle -M "Only file: $(=ls -AF)"
	fi
	if [[ ${#$(=ls -A)} -eq 0 ]] ; then
		zle -M "(Empty Directory)"
	fi
}
zle -N ls_if_emptybuf_or_expand
 
ignoreeof_three_times () {
	# do this only if buffer is empty
	[[ -z "${BUFFER}" ]] || return
 
  # sanity assignment
  if [[ "${LASTWIDGET}" != ignoreeof_three_times || -z "$ignoreeof_counter" ]] ; then
    ignoreeof_counter=0
  fi
 
  case "$ignoreeof_counter" in
    0) zle -M "I don't like you." ;;
    1) zle -M "Goodbye cruel world? Really??" ;;
    2) bye ;;
    *) zle -M "it is {$ignoreeof_counter} but whytf is it ??!?" ;;
  esac
 
  (( ignoreeof_counter++ ))
}
zle -N ignoreeof_three_times
Strumenti personali