zshrc ver.2

http://d.hatena.ne.jp/shrkw/20061012
PROMPT周りの設定方法がダサかったりしたのを直したりした。
ZSH - Documentationを初めて読んだ。部分的に読むだけならなかなかわかりやすいと思う。

  • hist_ignore_dups
  • hist_ignore_all_dups

これらの違いは前者は直前のコマンドのみ、後者はヒストリファイルのすべてのコマンドで重複を無視する。

# bindkey -v             # vi key bindings
bindkey -e             # emacs key bindings
# bindkey ' ' magic-space  # also do history expansion on space

## shell functions
#setenv() { export $1=$2 }  # csh compatibility

####################################################################
# enable contents assist at default
autoload -U compinit
compinit

####################################################################
# prompt settings
#
autoload -U colors
colors
setopt prompt_subst
PROMPT='%B%{${fg[yellow]}%}[%n@%m]%# %{${reset_color}%}%b'
RPROMPT='%B%{${fg[yellow]}%}[%~]%{${reset_color}%}%b'

if [ "$TERM" = "screen" ]; then
  PROMPT='%B%{${fg[yellow]}%}[%n@%m win:[$WINDOW]]%# %{${reset_color}%}%b'
  chpwd () { echo -n "^[_`dirs`^[\\" }
  preexec() {
    # see [zsh-workers:13180]
    # http://www.zsh.org/mla/workers/2000/msg03993.html
    emulate -L zsh
    local -a cmd; cmd=(${(z)2})
    case $cmd[1] in
      fg)
        if (( $#cmd == 1 )); then
          cmd=(builtin jobs -l %+)
        else
          cmd=(builtin jobs -l $cmd[2])
        fi
        ;;
      %*)
        cmd=(builtin jobs -l $cmd[1])
        ;;
      cd)
        if (( $#cmd == 2)); then
          cmd[1]=$cmd[2]
        fi
        ;&
      *)
        echo -n "^[k$cmd[1]:t^[\\"
        return
        ;;
    esac

    local -A jt; jt=(${(kv)jobtexts})

    $cmd >>(read num rest
      cmd=(${(z)${(e):-\$jt$num}})
      echo -n "^[k$cmd[1]:t^[\\") 2>/dev/null
  }
  chpwd
fi

precmd() {
    hostnam=${HOST##.*}     # wildcard, not regex!
    usernam=$(whoami)
    newPWD=${PWD}
    promptstr="--(${usernam}@${hostnam})-<mm/dd-hh:mm>---(${PWD})--"
    fillsize=$(( ${COLUMNS} - ${#promptstr} ))
    if [ $fillsize -ge 0 ]
    then
        fill=${(l.${fillsize}..-.)}
    else
        fill=""
        offset=$(( (${fillsize}*(-1)) + 4 ))
        newPWD="..."${newPWD[${offset},-1]}
    fi
}

termwide() {
    PROMPT=$YELLOW"-"$BLUE"-("$YELLOW"\${usernam}"$LIGHT_BLUE"@"$YELLOW"\${hostnam}"\
    $BLUE")-<"$YELLOW"%D{%m}"$LIGHT_BLUE"/$YELLOW%D{%d}"$BLUE"-"\
    $PURPLE"%D{%H}"$LIGHT_BLUE":"$PURPLE"%D{%M}"$BLUE">-"\
    $YELLOW"-\${fill}"$BLUE"-("$YELLOW"\${newPWD}"$BLUE")-"$YELLOW"-"\
    $'\n'\
    $YELLOW"-"$BLUE"-["\
    $YELLOW"%h"\
    $BLUE"]"$WHITE"%#"\
    $'%{\e[0m%} '

    PROMPT2=$LIGHT_BLUE"-"$YELLOW"-"$YELLOW"-"$LIGHT_GRAY$'%{\e[0m%} '
}

####################################################################
# around directory settings
#
setopt auto_cd
setopt auto_pushd # automatically pushd
setopt pushd_ignore_dups

####################################################################
# Set shell options
#
setopt magic_equal_subst # directory assist at command option input
setopt auto_menu # Automatically list choices on an ambiguous completion
setopt correct # command suggestion at typo
setopt auto_name_dirs
setopt auto_remove_slash # 
setopt rm_star_silent
setopt sun_keyboard_hack
setopt extended_glob # ファイル名で #, ~, ^ の 3 文字を正規表現として扱う
setopt list_types
setopt no_beep # no beep
setopt nolistbeep # no beep when complete list displayed
setopt always_last_prompt
setopt cdable_vars
setopt sh_word_split
setopt auto_param_keys
setopt list_packed # compacked complete list display

####################################################################
# history settings
#
setopt hist_ignore_all_dups
# setopt hist_ignore_dups # ignore dups only last command
setopt hist_ignore_space
setopt share_history
setopt inc_append_history # append history file soon
setopt extended_history

HISTFILE=/var/zsh-history
HISTSIZE=100000
SAVEHIST=1000000
function history-all { history -E 1 }

####################################################################
# alias
#
alias ls='ls -G'
alias -g L="| $PAGER"
alias -g M="| $PAGER"
alias -g G='| grep'
alias -g C='| cat -n'
alias -g W='| wc'
alias -g H='| head'
alias -g T='| tail'
alias -g ....='../..'