LoginSignup
59
58

More than 5 years have passed since last update.

zawでzshを便利に使う

Last updated at Posted at 2014-01-15

zawを使うとファイルオープン、ディレクトリ移動などがかなり簡単になる。

前提

  • perl インストール済み
  • zsh インストール済み
  • OSX 環境

準備

perlを入れておく。

zawを公式から clone してくる。

$ mkdir ~/zsh_plugins
$ cd zsh_plugins
$ git clone git://github.com/zsh-users/zaw.git
.zshrc
source ~/zsh_plugins/zaw/zaw.zsh
bindkey '^[d' zaw-cdr
bindkey '^[g' zaw-git-branches
bindkey '^[@' zaw-gitdir

function zaw-src-gitdir () {
  _dir=$(git rev-parse --show-cdup 2>/dev/null)
  if [ $? -eq 0 ]
  then
    candidates=( $(git ls-files ${_dir} | perl -MFile::Basename -nle \
                                               '$a{dirname $_}++; END{delete $a{"."}; print for sort keys %a}') )
  fi
  actions=("zaw-src-gitdir-cd")
  act_descriptions=("change directory in git repos")
}

function zaw-src-gitdir-cd () {
  BUFFER="cd $1"
  zle accept-line
}
zaw-register-src -n gitdir zaw-src-gitdir

zsh-gitdir については、
http://d.hatena.ne.jp/syohex/20120624/1340550154
の syohex さんのコードを使わせていただきました。ありがとうございます。

bindkey はご自由に。
Emacser 的に Ctrl+hoge は避けたいので、ここでは ^[ とすることで、Meta+hoge としている。

Meta は、 Ctrl+[ か Esc に該当。
iTerm2 ユーザなら、[Preferences]->[Profiles]->[Keys]->(Left|Right) options... を +Esc にすれば、optinon キーが Meta キーとなる。
(この設定は、 Emacs を emacs -nw で起動した場合にも有効なので Emacser は是非覚えておこう)

使い方

任意のディレクトリで、

  • Meta+d で cdr で履歴管理されているディレクトリ一覧が表示され、選択すると移動。

git 管理下のディレクトリで、

  • Meta+g で git のブランチ一覧表示、選択すると checkout
  • Meta+@ で カレントディレクトリが含まれる git 管理下(カレントより親も含む)のディレクトリ一覧表示、 選択すると移動

補足

cdr の設定はこんな感じにしている

autoload -Uz chpwd_recent_dirs cdr add-zsh-hook
add-zsh-hook chpwd chpwd_recent_dirs
zstyle ':chpwd:*' recent-dirs-max 5000
zstyle ':chpwd:*' recent-dirs-default yes
zstyle ':completion:*' recent-dirs-insert both

zaw はまだまた使いこなせてないので、便利な機能、設定があれば、是非コメントぶら下げてください。

59
58
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
59
58