LoginSignup
20
21

More than 5 years have passed since last update.

zshの設定ファイル

Last updated at Posted at 2014-05-18

自分の zsh の設定。

ディレクトリ構成

${HOME}/.zsh
 |-- .zshenv
 |-- .zshrc
 |-- .zsh_main
 |-- .zsh_option
 |-- .zsh_alias
 `-- .zsh_history

.zshenv

# ホームディレクトリからこのファイルにの名前でリンクを張る。
# つまり、${HOME}/.zshenv -> ${ZDOTDIR}/.zshenv 
export ZDOTDIR="${HOME}/.zsh"

.zshrc

# ${ZDOTDIR} を設定したので、このファイルにはリンクを張らなくても良い。
# 主に環境変数の設定
if [ -f ${ZDOTDIR}/.zsh_main ]; then
    source ${ZDOTDIR}/.zsh_main
fi

# 主に zsh 特有のオプションの設定
if [ -f ${ZDOTDIR}/.zsh_option ]; then
    source ${ZDOTDIR}/.zsh_option
fi

# 主にエイリアスの設定
if [ -f ${ZDOTDIR}/.zsh_alias ]; then
    source ${ZDOTDIR}/.zsh_alias
fi

.zsh_main

# path の設定

# たまに Solaris も使うので、そのパスも。
# (N-/) とすると存在するディレクトリだけをパスに加える。
path=(\
${HOME}/local/bin(N-/) \
/usr/local/bin(N-/) \
/usr/local/sbin(N-/) \
/opt/sfw/bin(N-/) \
/opt/sfw/sbin(N-/) \
/opt/csw/bin(N-/) \
/opt/csw/sbin(N-/) \
/usr/ccs/bin(N-/) \
/usr/sfw/bin(N-/) \
/usr/sfw/sbin(N-/) \
/usr/ucb(N-/) \
/usr/xpg4/bin(N-/) \
/usr/xpg6/bin(N-/) \
${path} \
)

# LD_LIBRARY_PATH でも (N-/) を使ったり、重複パスを削除したい(後述)。
if [ -z "${ld_library_path}" ]; then
    typeset -T LD_LIBRARY_PATH ld_library_path
fi
if [ -z "${ld_library_path_64}" ]; then
    typeset -T LD_LIBRARY_PATH_64 ld_library_path_64
fi

ld_library_path=(\
${HOME}/local/lib(N-/) \
/usr/local/lib(N-/) \
/usr/lib(N-/) \
/lib(N-/) \
/usr/sfw/lib(N-/) \
/usr/ucblib(N-/) \
/opt/sfw/lib(N-/) \
${ld_library_path} \
)

ld_library_path_64=(\
${HOME}/lib64(N-/) \
/usr/local/lib64(N-/) \
/usr/lib64(N-/) \
/lib64(N-/) \
/usr/sfw/lib/64(N-/) \
/usr/ucblib64(N-/) \
/opt/sfw/lib64(N-/) \
${ld_library_path_64} \
)

# 重複するパスを削除する。
typeset -U path ld_library_path ld_library_path_64
export PATH LD_LIBRARY_PATH LD_LIBRARY_PATH_64

# カーネル名。Linux や Solaris で設定を切り替えたいときに使用する。
export KERNEL_NAME="`uname -s`"

# 文字コード。横着して ${LANGUAGE} だけ変えれば良いように。
export LANGUAGE="ja_JP.eucJP"
export LANG="${LANGUAGE}"
export LC_ALL="${LANGUAGE}"

# エディタ、ページャ
export EDITOR="vim"
export CVSEDITOR="${EDITOR}"
export SVN_EDITOR="${EDITOR}"
export GIT_EDITOR="${EDITOR}"
export PAGER="less"

# ヒストリファイル
export HISTFILE="${ZDOTDIR}/.zsh_history"
export HISTSIZE=100000
export SAVEHIST=${HISTSIZE}

# 複数のグループを使っているので、環境変数で管理する。
export GROUP="`id -gn`"

# グループに応じて umask 値を変更する。
case "${GROUP}" in
    "hoge" | "piyo" | "fuga" )
        umask 027
        ;;
    "foo" | "bar" )
        umask 002
        ;;
    * )
        umask 022
        ;;
esac

# ターミナルの上の所に表示される文字列の設定。
# 普段は「グループ ユーザ名@ホスト名:カレントディレクトリ」
# tmux や screen を使っているときは「ユーザ名@ホスト名」
case "${TERM}" in
    xterm* | kterm* )
        precmd() {
            echo -ne "\033]0;[${GROUP}] ${USER}@${HOST}:${PWD}\007"
        }
        ;;
    screen* )
        precmd() {
            echo -ne "\033]0;${USER}@${HOST}\007"
        }
        ;;
esac

# 単語の区切りに / を追加する(デフォルトの WORDCHARS から / を取り除く)。
# C-w(単語削除)で便利。
export WORDCHARS="*?_-.[]~=&;!#$%^(){}<>"

# write すんな。
mesg n

.zsh_option

# zsh の補完候補が画面から溢れ出るとき、それでも表示するかどうか確認する。
export LISTMAX=0

# ls の色付け
# アーカイブファイルは赤
# 画像・動画・音声ファイルは紫
# pdf/ps/dvi は思案
# オフィス系は緑
# README/TODO/MEMO 系はオレンジ
export LS_COLORS="\
no=00:\
fi=00:\
di=01;34:\
ln=01;36:\
pi=40;33:\
so=01;35:\
do=01;35:\
bd=40;33;01:\
cd=40;33;01:\
or=40;31;01:\
su=37;41:\
sg=30;43:\
tw=30;42:\
ow=34;42:\
st=37;44:\
ex=01;32:\
*.tar=00;31:\
*.tgz=00;31:\
*.arj=00;31:\
*.taz=00;31:\
*.lzh=00;31:\
*.zip=00;31:\
*.z=00;31:\
*.Z=00;31:\
*.gz=00;31:\
*.bz2=00;31:\
*.bz=00;31:\
*.tbz2=00;31:\
*.tz=00;31:\
*.xz=00;31:\
*.deb=00;31:\
*.rpm=00;31:\
*.jar=00;31:\
*.rar=00;31:\
*.ace=00;31:\
*.zoo=00;31:\
*.cpio=00;31:\
*.7z=00;31:\
*.rz=00;31:\
*.eps=00;35:\
*.jpg=00;35:\
*.JPG=00;35:\
*.jpeg=00;35:\
*.JPEG=00;35:\
*.gif=00;35:\
*.GIF=00;35:\
*.bmp=00;35:\
*.BMP=00;35:\
*.pbm=00;35:\
*.pgm=00;35:\
*.ppm=00;35:\
*.tga=00;35:\
*.xbm=00;35:\
*.xpm=00;35:\
*.tif=00;35:\
*.tiff=00;35:\
*.png=00;35:\
*.PNG=00;35:\
*.mng=00;35:\
*.pcx=00;35:\
*.mov=00;35:\
*.mpg=00;35:\
*.mpeg=00;35:\
*.m2v=00;35:\
*.mkv=00;35:\
*.ogm=00;35:\
*.mp4=00;35:\
*.m4v=00;35:\
*.mp4v=00;35:\
*.vob=00;35:\
*.qt=00;35:\
*.nuv=00;35:\
*.wmv=00;35:\
*.asf=00;35:\
*.rm=00;35:\
*.rmvb=00;35:\
*.flc=00;35:\
*.avi=00;35:\
*.fli=00;35:\
*.gl=00;35:\
*.dl=00;35:\
*.xcf=00;35:\
*.xwd=00;35:\
*.yuv=00;35:\
*.aac=00;35:\
*.au=00;35:\
*.flac=00;35:\
*.mid=00;35:\
*.midi=00;35:\
*.mka=00;35:\
*.mp3=00;35:\
*.mpc=00;35:\
*.ogg=00;35:\
*.ra=00;35:\
*.wav=00;35:\
*.pdf=00;36:\
*.ps=00;36:\
*.dvi=00;36:\
*.doc=00;32:\
*.docx=00;32:\
*.xls=00;32:\
*.xlsx=00;32:\
*.ppt=00;32:\
*.pptx=00;32:\
*.potx=00;32:\
*.pot=00;32:\
*.odt=00;32:\
*.ods=00;32:\
*.odp=00;32:\
*.cvx=00;32:\
*README=00;33:\
*README.txt=00;33:\
*readme=00;33:\
*readme.txt=00;33:\
*TODO=00;33:\
*TODO.txt=00;33:\
*MEMO=00;33:\
*MEMO.txt=00;33:\
*memo=00;33:\
*memo.txt=00;33:\
"
export ZLS_COLORS="${LS_COLORS}"
export CLICOLOR="true"

# zsh の補完機能を強化する。必須。
autoload -Uz compinit
compinit

# プロンプト
# 色付きにする。
autoload -Uz colors
colors

# 上から順に、ユーザ名、ホスト名、カレントディレクトリ、グループ名、プロンプト
# プロンプトは、スーパーユーザ時に #
# スーパーユーザ以外かつ直前のコマンドの終了コードが 0 であるなら !, 0 でないなら $
p_usr="%{${fg[green]}%}%n%{${reset_color}%}"
p_hst="%{${fg[magenta]}%}@%m%{${reset_color}%}"
p_dir="%{${fg[cyan]}%}:%/%{${reset_color}%}"
p_grp="[${GROUP}]"
p_prt="%(?!%(!.#.$) !? )"
PROMPT="${p_usr}${p_hst}${p_dir}"$'\n'"${p_grp}${p_prt}"

# if ... fi とか "..." など、複数行にわたる場合
PROMPT2="%{${fg[yellow]}%}%_> %{${reset_color}%}"

# コマンド訂正時
SPROMPT="%{${fg[yellow]}%}correct%{${reset_color}%}: %{${fg[red]}%}%R%{${reset_color}%} => %{${fg[cyan]}%}%r%{${reset_color}%} (y|n|a|e)? "

# 右に出るプロンプト。日時を表示する。
RPROMPT="[%D{%m/%d %H:%M}]"

# Git を使っているときにリポジトリの状態を表示する。
# 非力なマシンだと表示に時間がかかるので、現状コメントアウト。
#autoload -Uz is-at-least
#if is-at-least 4.3.10; then
#   autoload -Uz vcs_info
#   zstyle ':vcs_info:git:*' check-for-changes true
#   zstyle ':vcs_info:git:*' stagedstr "%F{yellow}!"
#   zstyle ':vcs_info:git:*' unstagedstr "%F{red}+"
#   zstyle ':vcs_info:*' formats "%F{green}%c%u[%b]%f"
#   zstyle ':vcs_info:*' actionformats '[%b|%a]'
#   precmd () { vcs_info }
#   RPROMPT=$RPROMPT'${vcs_info_msg_0_}'
#fi

# ヒストリ探索
autoload -Uz 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 "^R" history-incremental-search-backward
bindkey "^S" history-incremental-search-forward
bindkey "^[[A" history-beginning-search-backward-end
bindkey "^[[B" history-beginning-search-forward-end

# キーバインドを emacs ライクに。vi ライクは -v
bindkey -e

# オプションシリーズ。ググったり、man を読んだりする。
setopt auto_list
setopt auto_menu
setopt auto_param_slash
setopt auto_pushd
setopt auto_remove_slash
setopt auto_resume
setopt brace_ccl
setopt complete_aliases
setopt correct
#setopt extended_glob
setopt extended_history
setopt hist_ignore_dups
setopt hist_ignore_all_dups
setopt hist_ignore_space
setopt hist_reduce_blanks
setopt hist_verify
setopt ignore_eof
setopt interactive_comments
setopt list_packed
setopt list_types
setopt magic_equal_subst
setopt mark_dirs
#setopt no_flow_control
setopt nobeep
setopt nolistbeep
setopt notify
setopt numeric_glob_sort
setopt print_eight_bit
setopt prompt_subst
setopt pushd_ignore_dups
setopt share_history
setopt zle
unsetopt glob_dots
unsetopt promptcr

# 補完候補の色付け、メッセージの設定
zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*' verbose yes
zstyle ':completion:*' completer _expand _complete _match _prefix _approximate _list _history
zstyle ':completion:*:messages' format "%{${fg[cyan]}%}%d%{${reset_color}%}"
zstyle ':completion:*:warnings' format "%{${fg[red]}%}no matches for: %{${fg[yellow]}%}%d%{${reset_color}%}"
zstyle ':completion:*:descriptions' format "%{${fg[yellow]}%}completing %d%{${reset_color}%}"
zstyle ':completion:*:corrections' format "%{${fg[yellow]}%}%d %{${fg[red]}%}(errors: %e){${reset_color}%}"
zstyle ':completion:*:options' description 'yes'
zstyle ':completion:*' group-name ''

# これらのファイルは補完候補にしない。でも rm では候補にする。
zstyle ':completion:*:*:(^rm):*:*files' ignored-patterns '*?.o|*?.d|*?.aux|*?~|*\#'

# 補完で大文字小文字を区別しない。
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}'

# 補完候補をカーソルで選ぶ。
zstyle ':completion:*:default' menu select=1

# cd 時にカレントディレクトリに候補がなかったら cdpath から選ぶ。
zstyle ':completion:*:cd:*' tag-order local-directories path-directories

# 補完時にこれらのディレクトリは除外する。
zstyle ':completion:*:cd:*' ignored-patterns '*CVS|*.svn|*.git|*lost+found'

# cd 時にカレントディレクトリを候補にしない。例えば、cd ../<TAB> とすれば分かる。
zstyle ':completion:*:cd:*' ignore-parents parent pwd

# kill 候補を色付け
zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([%0-9]#)*=0=01;31'

# math function を有効化する。zsh は浮動小数点を使えるのがいい。
zmodload -i zsh/mathfunc

.zsh_alias

# まずはデフォルトのエイリアスを全消し
unalias -m \*

# 基本的なエイリアス
alias c="clear" # C-l でも画面クリア可能
alias cdd="cd ../"
alias cddd="cd ../../"
alias cdddd="cd ../../../"
alias cp="cp -apir"
alias j="jobs -l"
alias less="less -IMx4 -X"
alias ls="ls -hF"
alias l="ls"
alias ll="ls -l"
alias lll="ls -l"
alias la="ls -a"
alias lal="ls -al"
alias lt="ls -lt"
alias ltr="ls -ltr"
alias mv="mv -i"
alias rm="rm -i"
alias rsync="rsync -a"
alias scp="scp -pr"
alias ssh="ssh -X"
alias src="source ${ZDOTDIR}/.zshrc"

# やっつけ…
grep -E --color=auto ZDOTDIR ${ZDOTDIR}/.zshenv > /dev/null 2>&1
if [ $? -eq 0 ]; then
    alias grep="grep -E --color=auto"
fi

ls -hF --show-control-chars --color=auto > /dev/null 2>&1
if [ $? -eq 0 ]; then
    alias ls="ls -hF --show-control-chars --color=auto"
fi

if type vim > /dev/null 2>&1; then
    alias vi="vim"
    alias vwo="vim -u NONE --noplugin" # まっさらな vim
    alias vl="vim -u ${HOME}/.vimrc.latex" # LaTeX 用
fi

if type lv > /dev/null 2>&1; then
    alias lv="lv -c"
    export PAGER="lv -c"
fi

if type nkf > /dev/null 2>&1; then
    alias nkfe="nkf -e --overwrite"
    alias nkfs="nkf -s --overwrite"
    alias nkfw="nkf -w --overwrite"
    alias nkfj="nkf -j --overwrite"
    alias nkfg="nkf -g"
fi

# これらはサフィックスエイリアス(後述)用のエイリアス
if type eog > /dev/null 2>&1; then
    alias imgv="eog"
else
    alias imgv="display"
fi

if type evince > /dev/null 2>&1; then
    alias pdfv="evince"
else
    alias pdfv="gv"
fi

if type soffice > /dev/null 2>&1; then
    alias officev="soffice"
else
    alias officev="ooffice"
fi

if type google-chrome > /dev/null 2>&1; then
    alias browser="google-chrome"
elif type firefox > /dev/null 2>&1; then
    alias browser="firefox"
else
    alias browser="w3m"
fi

# tmux の自動アタッチ設定は他の方のものを色々いじった。
if type tmux > /dev/null 2>&1; then
    alias tmux="tmux -2"
    # auto-attach if there are any tmux sessions 
    if ( ! test ${TMUX} ) \
    && ( ! expr ${TERM} : "^screen" > /dev/null 2>&1 ) \
    && ( test tmux > /dev/null 2>&1 ); then
        if tmux has-session > /dev/null 2>&1 ; then
            local session=`tmux list-sessions \
                          | grep -e "^[0-9].*]$" \
                          | head -n 1 \
                          | sed -e "s/^\([0-9]\+\).*$/\1/"`
            if [ -n "${session}" ]; then
                echo "Attach tmux session ${session}."
                tmux attach-session -t ${session}
            else
                echo "Session has been already attached."
                tmux list-sessions
            fi
        else
        # do nothing if there are no tmux sessions
        fi
    fi
elif type screen > /dev/null 2>&1; then
    # use 'screen' command as a substitute for 'tmux' command
    alias tmux="screen -xR"
fi

# ある文字列を含むファイルを再帰的に検索する。
# zsh なら grep **/* があるが、何故下記のようにしたか記憶に無い。
function fr() {
    # examples:
    # $ fr volatile             -> all files including "volatile"
    # $ fr "int main\(\)"       -> all files including "int main()"
    # $ fr volatile .c          -> *.c including "volatile"
    # $ fr "int main\(\)" .c    -> *.c including "int main()"
    find ./ -name "*$2" -type f | xargs grep -n "$1"
}

# 自動伸長。サフィックスエイリアスでも使う。
function myextract() {
    case "${KERNEL_NAME}" in
        "SunOS" )
            TAR="gtar"
            ;;
        "Linux" )
            TAR="tar"
            ;;
    esac

    for arg in $@
    do
        case ${arg} in
            *.tgz | *.tar.gz | *.tar.Z | *.tbz ) ${TAR} xzvf ${arg} ;;
            *.tbz2 | *.tar.bz2 ) ${TAR} xjvf ${arg} ;;
            *.tar.xz ) ${TAR} xJvf ${arg} ;;
            *.tar ) ${TAR} xvf ${arg} ;;
            *.zip ) unzip ${arg} ;;
            *.arj ) unarj ${arg} ;;
            *.lzh ) lha e ${arg} ;;
            *.gz ) gunzip ${arg} ;;
            *.bz2 ) bunzip2 ${arg} ;;
            *.xz ) xz -d ${arg} ;;
            *.Z ) uncompress ${arg} ;;
            * ) echo "Not Supported: ${arg}" ;;
        esac
    done
}
alias kaito="myextract"

# 自動圧縮。デフォルトは .tar.bz2
function mycompress() {
    case "${KERNEL_NAME}" in
        "SunOS" )
            TAR="gtar"
            ;;
        "Linux" )
            TAR="tar"
            ;;
    esac

    if [ $# -eq 2 ]; then
        case $1 in
            *.tgz | *.tar.gz ) ${TAR} czvf $1 $2 ;;
            *.tbz2 | *.tar.bz2 ) ${TAR} cjvf $1 $2 ;;
            *.tar.xz ) ${TAR} cJvf $1 $2 ;;
            *.tar ) ${TAR} cvf $1 $2 ;;
            *.zip ) zip -r $1 $2 ;;
            *.lzh ) lha a $1 $2 ;;
            * ) echo "Not Supported: $1" ;;
        esac
    elif [ $# -eq 1 ]; then
        echo "Create $1.tar.bz2 ... "
        ${TAR} cjvf $1.tar.bz2 $1 > /dev/null 2>&1
        echo "Done"
    fi
}
alias asshuku="mycompress"

# 画像を eps に変換する。
function img2eps() {
    for arg in $@
    do
        local filename=${arg}
        local filename_without_extension=${filename%.*}
        local pathname=${arg%/*}
        local extension=${arg##*.}
        if [ "${extension}" != "eps" ]; then
            echo -n "Convert ${filename} into eps file ... "
            convert ${filename} eps2:${filename_without_extension}.eps
            echo "Done"
        else
            echo "${filename}: eps file"
        fi
    done
}

# cd 後に ls する(zsh 特有)。
function chpwd() {
    ls
}

# まっさらな zsh を起動する。
alias zwo="env -i zsh -f"

# サフィックスエイリアス
# ファイルの拡張子によって実行するコマンドを自動判別する。
# 例:$ ./hoge.tar.gz -> $ myextract hoge.tar.gz -> $ tar xzvf hoge.tar.gz
alias -s dvi="xdvi"
alias -s pdf="pdfv"
alias -s {jpg,png,gif,bmp}="imgv"
alias -s {doc,docx,xls,xlsx,ppt,pptx,odt,ods,odp}="officev"
alias -s {htm,html}="browser"
alias -s {Z,gz,bz2,xz,tar,tgz,tbz,tbz2,zip,arj,lzh}="myextract"

# グローバルエイリアス
alias -g Y="yes |"
alias -g G="| grep"
alias -g V="| grep -v"
alias -g L="| less"
alias -g H="| head"
alias -g T="| tail"
alias -g S="| sort"
alias -g U="| uniq"
alias -g W="| wc"
alias -g X="| xargs"
alias -g LOG="2>&1 | tee"
alias -g WOA="> /dev/null 2>&1" # WithOut All
alias -g WOE="2> /dev/null" # WithOut stdErr
alias -g WOO="> /dev/null" # WithOut stdOut

# ディレクトリのエイリアス
# cd ~hoge のように使える。cd ~hoge の後に TAB を打てば補完も効く。
hash -d log="/var/log"
20
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
21