Skip to content

Instantly share code, notes, and snippets.

@metasta
Last active October 8, 2018 10:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save metasta/1752247 to your computer and use it in GitHub Desktop.
Save metasta/1752247 to your computer and use it in GitHub Desktop.
.zshrc
# .zshrc: users generic .zshrc file for zsh(1)
#
# This file is sourced only for interactive shells. It
# should contain commands to set up aliases, functions,
# options, key bindings, etc.
#
# Global Order: zshenv, zprofile, zshrc, zlogin
## General
#
LANG=ja_JP.UTF-8
watch=all
setopt ignoreeof # ignore ctrl-D (max: 10 times)
setopt nobeep # silent
WORDCHARS='*?_-.[]~=&;!#$%^(){}<>' # exclude '/' for backward-kill-word
REPORTTIME=3 # report elapsed time (> 3 sec.)
## Prompt
#
PROMPT='%F{10}%n@%m%f:%F{12}%4(~,%-1~/.../%1~,%~)%f
%0(?.%#.%F{9}%#%f) '
RPROMPT='%*'
case $TERM in
xterm*|rxvt*)
function _update_title(){print -Pn '\e]0;%n@%m:%4(~,%-1~/.../%1~,%~)\a'}
precmd_functions+=(_update_title) ;;
esac
## Changing Directory
#
# cd -[tab] (do not push duplicates, reverse order)
setopt autopushd pushdignoredups pushdminus
# list directory contents after each cd
function _autols(){print -P "%F{8}$(COLUMNS=$COLUMNS command ls -CF)%f"}
chpwd_functions+=(_autols)
## History
#
HISTFILE="$ZDOTDIR/.history"
HISTSIZE=50000
SAVEHIST=50000
setopt extendedhistory # save timestamp and duration
setopt histignorealldups # ignore duplicates
setopt histignorespace # ignore lines which start with space
setopt incappendhistory # incremental append
setopt sharehistory # share history between zsh processes
## Completion
#
fpath+=($ZDOTDIR/completion(N-/))
autoload -Uz compinit && compinit -d "$ZDOTDIR/.zcompdump"
zstyle ':completion:*' completer _expand _complete _correct _approximate
zstyle ':completion:*' format '%F{8}# %d%f' # group title
zstyle ':completion:*' group-name '' # show all groups
zstyle ':completion:*' list-separator '' # default '--'
zstyle ':completion:*' list-colors '=*=01;30' # color=gray
zstyle ':completion:*' matcher-list '' 'm:{a-z}={A-Z}' '+m:{A-Z}={a-z}' 'r:|[._-]=* r:|=* l:|=*'
zstyle ':completion:*' use-cache true
zstyle ':completion:*' verbose true
zstyle ':completion:*' menu select=2
zstyle ':completion:*:functions' ignored-patterns '_*'
setopt listpacked # smaller completion list
setopt menucomplete # insert matches immediately
setopt extendedglob globdots nocaseglob # advanced filename generation
## Keybind
#
setopt noflowcontrol # disable flow control (for ^S/^Q shortcut)
bindkey -e # emacs keybind
bindkey '\e[Z' reverse-menu-complete # [shift]+[tab]
bindkey '^F' forward-word
bindkey '^B' backward-word
bindkey '^R' history-incremental-pattern-search-backward
bindkey '^S' history-incremental-pattern-search-forward
bindkey '^J' run-help
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
## Functions
#
autoload -Uz zmv
## Alias
#
if type dircolors >/dev/null
then # GNU ls
alias ls='command ls --color=auto -F' la='ls -a' ll='ls -Ahl'
else # BSD ls
alias ls='command ls -FG' la='ls -a' ll='ls -AOhl' le='ll -e@'
fi
alias cp='cp -pi'
alias mv='mv -i'
alias df='df -h'
alias du='du -h'
alias su='su -l'
alias grep='grep --color=auto'
alias diff='diff -U 0'
alias less='LESSHISTFILE=- less --tabs=2'
alias sudo='sudo -E '
if type osascript >/dev/null 2>&1
then
alias js='osascript -l JavaScript'
fi
# testing/temporary/machine-specific settings
test -r "$ZDOTDIR/.zshrc.mine" && source "$ZDOTDIR/.zshrc.mine" || true

My zshrc

一部で「究極のシェル」と名高い zsh の設定ファイル .zshrc

(旧 .zshrcこちら)

Setup

ディレクトリ構成

$HOME/
  ├─.zshenv
  └─.zsh/
      ├─.zshrc
      ├─.zcompdump
      └─.history

関連ファイルをホームディレクトリにばら撒かず .zsh/ に集めることにする. そのために環境変数 ZDOTDIR.zshenv ファイル中で指定する.

% mkdir ~/.zsh
% echo 'ZDOTDIR="$HOME/.zsh"' >> ~/.zshenv

これで .zshenv 以外の設定ファイル (.zshrc, .zprofile など) を ~/ ではなく ~/.zsh/ に置くことができる.

もし .zshenv もホームディレクトリに置きたくないのであれば, .zshenv ではなく /etc/zshrc

ZDOTDIR="$HOME/.zsh"

と設定すればよい. その場合のディレクトリ構成は以下の通り.

$HOME/
  └─.zsh/
      ├─.zshenv
      ├─.zshrc
      ├─.zcompdump
      └─.history

Keybind

とくに便利と感じるもの (無印は bindkey -e による設定、* はカスタム設定)

  • [tab] expand-or-complete とにかく頻用. 気がつけば連打
  • [shift]+[tab] reverse-menu-complete(*) [tab] と逆順に選ぶ
  • [control]+a beginning-of-line カーソルを行頭へ移動
  • [control]+e end-of-line カーソルを行末へ移動
  • [control]+f forward-word(*) カーソルを単語ひとつぶん後ろへ移動
  • [control]+b backward-word(*) カーソルを単語ひとつぶん前へ移動
  • [control]+w backward-kill-word 単語ひとつぶん削除
  • [control]+k kill-line カーソルから行末まで削除
  • [control]+q push-line 入力中のコマンドを保持して新しいコマンドラインを用意する
  • [control]+p history-beginning-search-backward-end(*) 履歴を検索 (新しい方から)
  • [control]+n history-beginning-search-forward-end(*) 履歴を検索 (古い方から)
  • [control]+r history-incremental-pattern-search-backward(*) 履歴をインクリメンタル検索 (新しい方から)
  • [control]+s history-incremental-pattern-search-forward(*) 履歴をインクリメンタル検索 (古い方から)
  • [control]+j run-help(*) 入力中のコマンドを保持して man を開く
  • [control]+l clear-screen 画面をクリア (clearコマンドと同じ)

^F, ^B の挙動はデフォルトでは [esc]+f, [esc]+b. 自分は [esc] より押しやすい [control] に割りあてた.

ちなみに

% bindkey -L

でキーバインドの一覧を見ることができる.

Changelog

2016-09-22

  • remove: log を削除(いつからか zsh のコマンドではなくなってた)

2014-01-28

  • add: 自作補完関数の置き場所 (.zsh/completion) の設定を追加

2013-10-20

  • add: alias less を追加、LESSHISTFILE の設定を移動

2013-10-08

  • add: alias sudo を追加
  • modify: alias ls を変更 (OS X での -O, -e, -@ オプションを追加)

2013-02-07

  • remove: alias nano を削除 (漢なら vi)

2013-02-03

  • modify: alias nano の内容を変更

2012-09-16

  • modify: 一部 Keybind を変更
    • ^F, ^B のカーソル移動を 1 字単位から 1 語単位へ変更
    • ^R, ^S の履歴検索にワイルドカードを使用可能にした
    • ^H を run-help からデフォルト (backward-delete-char) に変更
    • ^J を accept-line (return) から run-help に変更

2012-09-12

  • 書き直し (旧 .zshrcこちら)
    • add: cd 直後に ls を実行
    • add: 直前のコマンドの終了ステータスをプロンプトの色で表示
    • add: 実行に時間を要したコマンドの直後に elapsed time を表示
    • add: 補完候補を大幅に増量
    • modify: 設定ファイル等の置き場所を変更
    • modify: プロンプト記号を $ から % に変更
    • modify: time の出力書式を変更 (bash 風にカスタマイズしてたのを戻した)
    • modify: 補完結果の配色を ls の配色と合せてたけどグレー単色に変更
    • modify: ls の配色を変更 (Mac, Linux で統一してたけど各環境のデフォルトに戻した)
    • remove: コマンド訂正機能 (correct) を削除 (代りに補完機能を強力にした)
    • remove: ディレクトリ名で自動的に cd する機能 (autocd) を削除 (全然使ってなかった)
    • remove: completealiases オプションを削除 (エイリアスを元のコマンドとは別物とみなすので、適切な補完が効かなかった)
    • remove: 一部 Keybind (Home, Del, End) を削除
  • ドキュメント追加
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment