nikuyoshiのブログ

記事の内容については個人の見解であり、所属企業の意見を代表するものではありません

Macのシェルをzshに変更した

以下のサイトを参考にしてzshの設定を行いました。*1 *2
UKSTUDIO - Macのログインシェルをzshに変更する
漢のzsh | コラム | エンタープライズ | マイナビニュース

zshの設定

■プロンプトの色を参考にしたサイト

cuspy memo - zsh colors

■プロンプトの変数に関して参考にしたサイト
zsh: 13. Prompt Expansion

今までのシェルでも事足りてたけど、補完機能に魅力を感じました。実際使ってみたけど、こりゃいいわ。
~/.bashrcで設定してあったMacVimのエイリアスも通しています。

こんな感じです。
f:id:nikuyoshi:20111207224020p:image
以下にソースを記述します。
~/.zshrc

# users generic .zshrc file for zsh(1)

## Environment variable configuration
#
# LANG
#
export LANG=ja_JP.UTF-8

## Default shell configuration
#
# set prompt
#
setopt prompt_subst

if [ $USER = "root" ] 
then
    PROMPT="%{e[$[31]m%}%B$LOGNAME@%m[%W %T]:%b%{e[m%} # "
    RPROMPT="[%{e[31m%}%~%{e[m%}]"
    PATH=${PATH}:/sbin:/usr/sbin:/usr/local/sbin
    HOME=/root
else
#    PROMPT="%{e[33m%}$LOGNAME@%m%B[%W %T]:%b%{e[m%} %% "
    PROMPT="%{e[$[32+$RANDOM % 5]m%}$LOGNAME@%m%B[%W %T]:%b%{e[m%} %% "
    RPROMPT="[%{e[33m%}%~%{e[m%}]"
fi

# auto change directory
#
setopt auto_cd

# auto directory pushd that you can get dirs list by cd -[tab]
#
setopt auto_pushd

# command correct edition before each completion attempt
#
setopt correct

# compacked complete list display
#
setopt list_packed

# no remove postfix slash of command line
#
setopt noautoremoveslash

# no beep sound when complete list displayed
#
setopt nolistbeep

## Keybind configuration
#
# emacs like keybind (e.x. Ctrl-a goes to head of a line and Ctrl-e goes 
# to end of it)
#
bindkey -e

# historical backward/forward search with linehead string binded to ^P/^N
#
autoload history-search-end
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end
bindkey "^p" history-beginning-search-backward-end
bindkey "^n" history-beginning-search-forward-end
bindkey "\\ep" history-beginning-search-backward-end
bindkey "\\en" history-beginning-search-forward-end

## Command history configuration
#
HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000
setopt hist_ignore_dups # ignore duplication command history list
setopt share_history # share command history data

## Completion configuration
#
autoload -U compinit
compinit

## Alias configuration
#
# expand aliases before completing
#
setopt complete_aliases # aliased ls needs if file/dir completions work

alias where="command -v"
alias j="jobs -l"

case "${OSTYPE}" in
freebsd*|darwin*)
  alias ls="ls -G -w"
  ;;
linux*)
  alias ls="ls --color"
  ;;
esac

alias la="ls -a"
alias lf="ls -F"
alias ll="ls -l"

alias du="du -h"
alias df="df -h"

alias su="su -l"

## terminal configuration
#
unset LSCOLORS
case "${TERM}" in
xterm)
  export TERM=xterm-color
  ;;
kterm)
  export TERM=kterm-color
  # set BackSpace control character
  stty erase
  ;;
cons25)
  unset LANG
  export LSCOLORS=ExFxCxdxBxegedabagacad
  export LS_COLORS='di=01;34:ln=01;35:so=01;32:ex=01;31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
  zstyle ':completion:*' list-colors \
    'di=;34;1' 'ln=;35;1' 'so=;32;1' 'ex=31;1' 'bd=46;34' 'cd=43;34'
  ;;
esac

# set terminal title including current directory
#
case "${TERM}" in
kterm*|xterm*)
  precmd() {
    echo -ne "\033]0;${USER}@${HOST%%.*}:${PWD}\007"
  }
  export LSCOLORS=exfxcxdxbxegedabagacad
  export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=46;34:cd=43;34:su=41;30:sg=46;30:tw=42;30:ow=43;30'
  zstyle ':completion:*' list-colors \
    'di=34' 'ln=35' 'so=32' 'ex=31' 'bd=46;34' 'cd=43;34'
  ;;
esac

## load user .zshrc configuration file
#
[ -f ~/.zshrc.mine ] && source ~/.zshrc.mine

*1:2011/12/7更新 プロンプト部分を変更しました。

*2:2011/12/8更新 MacVimのエイリアスを.zshenvに移し変えました。