Top > Linux > zsh

Linux/zsh
HTML ConvertTime 0.015 sec.

.zshrc [2018.03.03] Edit

##:
##: Rev:   : 20090211, 20100801, 20110507, 20140215, 20150401, 20151129, 20160120, 20160124, 20160213, 20160216, 20161112, 20171009, 20171020, 20171104
##: CentOS : 5.2, 5.5, 5.6, 6.2, 6.5, 6.7, 6.8, 6.9
##: zsh    : 4.2.6-1, 4.3.10, 4.3.11
##:
##: History: 2017.10.07: Set "menu select interactive".

#
# .zshrc is sourced in interactive shells.
# It should contain commands to set up aliases,
# functions, options, key bindings, etc.
#


autoload -Uz compinit
compinit -u                         # 2017.10.07: -u option
autoload -Uz colors
colors

setopt always_last_prompt
setopt append_history
setopt auto_cd
setopt auto_pushd
setopt auto_remove_slash
setopt brace_ccl
setopt complete_in_word
setopt extended_glob
setopt hist_ignore_space
setopt inc_append_history
setopt interactive_comments
setopt listpacked
setopt long_list_jobs
setopt magic_equal_subst
setopt no_beep
#setopt no_clobber                  # 2014.09.27: Usage: echo 1 >| file
setopt no_flow_control
setopt print_eight_bit
setopt prompt_subst
setopt pushd_ignore_dups
setopt share_history
setopt hist_ignore_all_dups
setopt no_nomatch                   # 2012.10.25
unsetopt promptcr
setopt extended_history             # 2015.11.29
setopt list_packed                  # 2017.10.21

LISTMAX=0

HISTFILE=~/.zsh_history
HISTSIZE=10000
SAVEHIST=10000

#zstyle ':completion:*:default' menu select=2
zstyle ':completion:*:default' menu select interactive
zstyle ':completion:*' menu select interactive
#setopt menu_complete

zstyle ':completion:*' list-colors ${(s.:.)LS_COLORS}
zstyle ':completion:*:cd:*' ignore-parents parent pwd           # 2015.11.29
# cd 6[TAB] -> cd xxx6xxx  (2017.10.02)
#zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
# ll H -> ll Http  (2017.10.20)
#   http://gihyo.jp/dev/serial/01/zsh-book/0005
zstyle ':completion:*' matcher-list '' 'm:{a-zA-Z}={A-Za-z}' '+m:{A-Z}={a-z}' 'r:|[._-,]=** r:|=**' 'l:|=** r:|=**'
# History (M-, or M-/)  (2017.10.02)
#zstyle ':completion:*:history-words' list no
zstyle ':completion:*:history-words' menu yes
zstyle ':completion:*:history-words' remove-all-dups yes
zstyle ':completion:*:history-words' stop yes

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 -e
bindkey '^?'    backward-delete-char
bindkey '^H'    backward-delete-char
bindkey '^[^k'  backward-kill-line          # 2017.10.21: C-M-k
bindkey '^[[3~' delete-char
bindkey '^[[1~' beginning-of-line
bindkey '^[[4~' end-of-line
bindkey '^x^b'  vi-backward-blank-word      # 2017.10.07: C-x C-b
bindkey '^]'    vi-find-prev-char           # 2017.10.10: C-]
bindkey '^[^]'  vi-find-next-char           # 2017.10.10: C-M-]

#WORDCHARS='*?_-.[]~=&;!#$%^(){}<>'
WORDCHARS='*?_-.[]~&;!#$%^(){}<>'           # 2017.10.20

typeset -U path PATH                        # 2017.10.22: Delete duplicate path/PATH

autoload -Uz zmv                            # 2014.09.27
alias zmv='noglob zmv -vW'

autoload -Uz smart-insert-last-word         # 2015.11.29
zle -N insert-last-word smart-insert-last-word

autoload -Uz edit-command-line              # 2015.11.29
zle -N edit-command-line
bindkey '^xe' edit-command-line             # C-x e

# Time Command
# http://qiita.com/dealforest/items/eb9e6a7ed30769adae62
REPORTTIME=1                                # 2015.11.29

PROMPT=$(print "%B%{\e[34m%}%m%# %b")
#RPROMPT=' %~'
#RPROMPT=' %~ [%*]'                         # %*:12:34:56, %T: 12:34
RPROMPT=' %~ [%F{green}%*%f]'               # Color: %F{greep} xxx %f
#
#PROMPT=$(print "%B%{\e[34m%}%m:%(5~,%-2~/.../%2~,%~)%# %b")
#PROMPT=$(print "%B%{\e[34m%}%m:%(5~,%-2~/.../%2~,%~) %# %b")

## bindkey -s  (2010.08.22)
#    http://gihyo.jp/dev/serial/01/zsh-book/0003?page=3
#    Usage: M-A
bindkey -s '^[A' "| awk '{print $}'|^B^B^B"
#    Usage: M-L
#bindkey -s '^BbBb[L' "^E|less^M"
# ...


alias -g M='|less'
#alias -g M='|lv'
alias -g H='|head'
alias -g T='|tail'
alias -g G='|egrep -i'
alias -g W='|wc -l'
alias -g X='|xargs'
alias -g S='|sort'
alias -g U='|uniq'
alias ls='ls -F --color=auto'
alias l='ls'
alias ll='ls -l'
alias lt='ll -tr'
alias lta='ll -trA'
alias l.='ls -A'
alias la='ls -A'
alias lla='ll -A'
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias m='less'
#alias m=lv
alias g='egrep -i'
#alias h='history -'            # Usage: history first last, history -50, fc -l -50
alias ...='cd ../..'
alias mu='emacs -nw'
alias mun='emacsclient -nw'
alias vi='vim'                  # 2016.01.20
alias sc='screen'
alias sl='screen -list'
alias diffs='diff -y --left-column --suppress-common-lines'     # same as "sdiff -ls"  (2014.01.20)
alias diff='colordiff'
alias tree='tree -aCF'

export GREP_COLOR='00;31'
export GREP_OPTIONS='--color=auto'
#export EDITOR=vim                                                  # 2016.02.13, 2016.02.16: NG
export LESSEDIT='vim ?lm+%lm. %f'                                   # 2016.02.16
export LESS='-R --tabs=8 --no-init --LONG-PROMPT --ignore-case'     # 2014.09.27

## screen bash -> zsh
export SHELL=zsh

## YYYYmmdd
YYY=$(date '+%Y%m%d')

## Emacs Shell & Tramp
case "$TERM" in
        # Emacs shell
        dumb | emacs)
                alias ls='ls -F'
                PROMPT='%n@%m:%~$ '
                #PROMPT="%m:%~> "
                unsetopt zle
                ;;
esac

## hash  (2017.10.17)
#    Usage: cd ~log
hash -d log=/var/log
hash -d mes=/var/log/messages
hash -d .z=/root/.zshrc
hash -d .e=/root/.emacs


## history  (2010.08.22, 2014.01.20)
#    Usage: h, h 50
#       -d: Time stamp
function h () {
    case "$#" in
        0) history -d - ;;
        1) history -d -$1 ;;
        *) "Error:  Usage: h, h 50" ;;
    esac
}

## mkdir & cd  (2010.08.22)
#    Usage: mkcd dir
function mkcd () {
    case "$#" in
        1) /bin/mkdir -p "$@" && cd "$*[-1]" ;;
        *) "Error:  Usage: mkcd dir" ;;
    esac
}

## file copy  (Mod: 2012.01.09, 2016.11.12)
#    cp -a source backup
#    Usage: cpf hoge .20110331    -> hoge.20110331
#           cpf hoge .20110331 r0 -> hoge.20110331r0
function cpf () {
    case "$#" in
        2) cp -a ${1} ${1}${2} ;;
        3) cp -a ${1} ${1}${2}${3} ;;
        *) echo "Error:  Usage: cpf hoge .20110331    -> hoge.20110331"
           echo "               cpf hoge .20110331 r0 -> hoge.20110331r0" ;;
    esac
}

## file rename  (Mod: 2014.09.27, 2016.11.12)
#    mv source rename
#    Usage: mvf hoge .20110331    -> hoge.20110331
#           mvf hoge .20110331 r0 -> hoge.20110331r0
function mvf () {
    case "$#" in
        2) mv ${1} ${1}${2} ;;
        3) mv ${1} ${1}${2}${3} ;;
        *) echo "Error:  Usage: mvf hoge .20110331    -> hoge.20110331"
           echo "               mvf hoge .20110331 r0 -> hoge.20110331r0" ;;
    esac
}

## Regexp ls  (2012.11.02)
#    Usage: llg \^.emacs$
function llg () {
    /bin/ls -A | grep -E "$@" | xargs ls -ldF
}

## Time-stamped Copy  (2015.03.21, 2016.11.12)
#    I put a time stamp in the file name
#    Usage: cpt file1     -> file1_20110101
#           cpt file1 _r1 -> file1_20110101_r1
function cpt () {
    local _W_=',,'      # Set Word
    case "$#" in
        1|2) cp -a $1 $1${_W_}`ll --full-time $1 | awk -F" " '{print $6}' | sed -e s/-//g`$2 ;;
        *  ) echo "Error:  Usage: cpt file1     -> file1,,20110101"
             echo "               cpt file1 _r1 -> file1,,20110101_r1" ;;
    esac
}

## Peco  (2016.02.11, 2017.10.01)
#    GitHub - peco/peco: Simplistic interactive filtering tool
#    https://github.com/peco/peco
#    Binary: https://github.com/peco/peco/releases/
#    wget https://github.com/peco/peco/releases/download/v0.5.1/peco_linux_amd64.tar.gz
if [ `which peco 2> /dev/null` ]; then
    function peco-select-history() {
        local tac
        if which tac > /dev/null; then
            tac="tac"
        else
            tac="tail -r"
        fi
        BUFFER=$(\history -n 1 | \
            eval $tac | \
            peco --query "$LBUFFER")
        CURSOR=$#BUFFER
        zle clear-screen
    }
    zle -N peco-select-history
    #bindkey '^r' peco-select-history
    alias -g P='|peco'
    alias p=peco
fi

## cdr  (2016.02.11)
#    man zshcontrib
autoload -Uz is-at-least
if is-at-least 4.3.11
then
    autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
    add-zsh-hook chpwd chpwd_recent_dirs
    zstyle ':chpwd:*' recent-dirs-file "$HOME/.zsh_chpwd-recent-dirs"
    zstyle ':chpwd:*' recent-dirs-max 500
    zstyle ':chpwd:*' recent-dirs-default true
    zstyle ':chpwd:*' recent-dirs-pushd true                # 2017.10.09
    zstyle ':completion:*' recent-dirs-insert both
    zstyle ':completion:*:*:cdr:*:*' menu selection
fi

## cdd  (2017.09.30)
#    http://m4i.hatenablog.com/entry/2012/01/26/064329
#    https://qiita.com/yoshikaw/items/5a2ac8f1feed8d708d40
#    git clone git://github.com/m4i/cdd
if [ `which cdd 2> /dev/null` ]; then
    #autoload -Uz compinit && compinit
    #zstyle ':completion:*' menu select
    #zstyle ':completion:*' menu select interactive
    #zstyle ':completion:*:descriptions' format '%BCompleting%b %U%d%u'
    zstyle ':completion:*:descriptions' format '%F{red}Completing %B%d%b%f'
    source /usr/local/bin/cdd
    typeset -ga chpwd_functions
    chpwd_functions+=_cdd_chpwd
fi

## set -o  (2017.10.02)
#    See. setopu / unsetopt
#    https://qiita.com/mollifier/items/26c67347734f9fcda274
function set-o_showoptions () {
    set -o | sed -e 's/^no\(.*\)on$/\1  off/' -e 's/^no\(.*\)off$/\1  on/'
}

## zman  (2017.10.20)
#    https://qiita.com/mollifier/items/14bbea7503910300b3ba
function zman () {
    PAGER="less -g -s '+/^       "$1"'" man zshall
}

# ## Menu jkhl  (2017.10.10)
# #    補完時にhjklで選択
# #    memozsh の補完メニューを hjkl で移動 - Qiita
# #    https://qiita.com/mtsmfm/items/5e6aeab4acdc56521b2f
# zmodload -i zsh/complist
# autoload -Uz compinit && compinit
# zstyle ':completion:*:default' menu select=2
# bindkey -M menuselect 'j' vi-down-line-or-history
# bindkey -M menuselect 'k' vi-up-line-or-history
# bindkey -M menuselect 'l' vi-forward-char

## zshのメニュー補完で候補をインタラクティブに絞り込む - Qiita  (2017.10.07)
#    https://qiita.com/ToruIwashita/items/5cfa382e9ae2bd0502be
#autoload -Uz compinit && compinit -u
#zstyle ':completion:*' menu select interactive
#setopt menu_complete
zmodload zsh/complist                                         # "bindkey -M menuselect"設定できるようにするためのモジュールロード
bindkey -M menuselect '^g' .send-break                        # send-break1回分の効果
bindkey -M menuselect '^i' forward-char                       # 補完候補1つ右へ
bindkey -M menuselect '^j' .accept-line                       # accept-line2回分の効果
bindkey -M menuselect '^k' accept-and-infer-next-history      # 次の補完メニューを表示する
bindkey -M menuselect '^n' down-line-or-history               # 補完候補1つ下へ
bindkey -M menuselect '^p' up-line-or-history                 # 補完候補1つ上へ
bindkey -M menuselect '^r' history-incremental-search-forward # 補完候補内インクリメンタルサーチ
bindkey -M menuselect '^s' history-incremental-search-forward # 補完候補内インクリメンタルサーチ

## zaw  (2017.10.17)
#    git clone git://github.com/zsh-users/zaw.git
#      Installed:
#        cd ~/zsh
#        git clone git://github.com/zsh-users/zaw.git
#      Description:
#        Trigger zaw by pressing Ctrl-x ; (^x;)
#        $ zaw-print-src
#        Enter:           accept-line (execute default action)
#        Meta + Enter:    accept-search (execute alternative action)
#        Tab:             select-action
if [ -f ~/zsh/zaw/zaw.zsh ]; then
    source ~/zsh/zaw/zaw.zsh
    zstyle ':filter-select:highlight' matched fg=yellow,bg=black,standout
    #zstyle ':filter-select' max-lines 10           # use 10 lines for filter-select# 10行候補を表示する
    zstyle ':filter-select' max-lines -20           # use $LINES - 10 for filter-select# 元の表示を10行残す
    zstyle ':filter-select' rotate-list yes         # enable rotation for filter-select
    zstyle ':filter-select' case-insensitive yes    # enable case-insensitive search
    zstyle ':filter-select' extended-search yes     # see below
    zstyle ':filter-select' hist-find-no-dups yes   # ignore duplicates in history source
    zstyle ':filter-select' escape-descriptions no  # display literal newlines, not \n, etc
    bindkey '^[@' zaw-cdr       # M-@
    bindkey '^xd' zaw-cdr       # C-x d
    bindkey '^[r' zaw-history   # M-r
    bindkey '^x]' zaw-history   # C-x ]
fi

## fzf  (2017.10.22)
#    GitHub - junegunn/fzf: A command-line fuzzy finder~
#    https://github.com/junegunn/fzf
#    fzf - FreeBSD入門
#    http://kaworu.jpn.org/freebsd/fzf
#    Installed:
#      git clone --depth 1 https://github.com/junegunn/fzf.git ~/zsh
#      ~/zsh/fzf/install
#    Error: fzf-completion:32: invalid length: -2
#      https://github.com/junegunn/fzf/issues/665
#      *** CentOS 6.x / zsh-4.3.x ***
#        vi fzf/shell/completion.zsh
#    Usage:
#      ls **<TAB>, ls /etc/ss**<TAB>
#      ssh **<TAB>
#      C-r   : CTRL-R - Paste the selected command from history into the command line
#      C-t   : CTRL-T - Paste the selected file path(s) into the command line
#      M-c   : ALT-C - cd into the selected directory
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh





## Command Sample  (2012.11.02, 2016.01.24)
#    ll *(m-1)                 # Files that have been updated within one day
#    ll *(mh-12)               # Files that have been updated within 12 hours
#    ll **/*host*
#    ll /etc/defaults/**/.*    # Target defaults directory
#    ll a<3-7>.txt
#    touch file_{001..050}
#    rm *~hoge.txt             # Specify the non-hoge.txt
#    rm *~*<3-7>.txt
#    rm *~(*sh|*3*|*<6-9>*)
#    ll **/*(Lm+10)            # Files larger than 10MB
#    ll *(.)                   # Specify the file
#    ll -d *(/)                # Specify the directory
#    ll -d *(^/)               # Exclude the directory
#    ll -d *(D)                # Specify all
#    ll **/*(D^/)              # specify all other directories
#    ll **/(.*|*)(^/)          # specify all other directories
#    grep hoge **/*(.D)        # Specify the file
#
#    zmv *.txt file-*.log      # -n: no execution: just print what would happen
#
#    hash -d log=/var/log      # cd ~log