mfks17's blog(Life is Good !!)

趣味や思った事を書いていくと思います

ディレクトリ移動を簡単に!!zのインストール

zのgithubリポジトリ

zとは、githubより

DESCRIPTION
       Tracks your most used directories, based on 'frecency'.

       After  a  short  learning  phase, z will take you to the most 'frecent'
       directory that matches ALL of the regexes given on the command line.

説明
  'frecency'に基づいて、あなたの最も使用されているディレクトリを、追跡します。

  短い学習段階の後に、コマンドライン上から指定された正規表現のすべてに一致する
  最も 'frecent'なディレクトリに移動できます。(英語力がががが、)

インストール


今回は、brewで入れました

brew install z

そうすると、

==> Downloading https://github.com/rupa/z/tarball/v1.4
######################################################################## 100.0%
==> Caveats
For Bash or Zsh, put something like this in your $HOME/.bashrc or $HOME/.zshrc:

  . `brew --prefix`/etc/profile.d/z.sh

ZSH USERS BACKWARD COMPATIBILITY WARNING:
 z now handles 'precmd' set up for zsh. z (<=1.3) users using zsh should
 remove the precmd function that was described in the installation
 instructions for previous versions.

In short, this:
  . /path/to/z.sh
  function precmd () {
   _z --add "$(pwd -P)"
  }

should now be:
  . `brew --prefix`/etc/profile.d/z.sh
==> Summary
&#127866;  /usr/local/Cellar/z/1.4: 4 files, 28K, built in 5 seconds

こんな感じで入ります。

そして、言われた通りに設定を追加します。

$ vi ~/.bashrc

## z easy jump dir
. `brew --prefix`/etc/profile.d/z.sh
    function precmd () {
    _z --add "$(pwd -P)"
}

こんな感じで。
そうしたら、シェルに再ログインしたら使えます。

仕組み(おそらく、、、)

移動の履歴が.zというファイルに記憶(同じ場所にいった場合は、上書きされる)
設定を変えるときは、環境変数である$_Z_DATAの設定をすれば変更可能らしい。
そのファイルをもとにごにょごにょするらしい。(すいません、あとでman読みますm(_ _)m)

.zの中身

/Users/typosterr/Downloads|10|1359175520
/Users/typosterr/Development|9|1359172300
/Users/typosterr/Development/google_appengine_go|7|1359175270

パスの後ろの数値がそれぞれrank,timeです。
rankはそこに行った回数で、timeは最後に行った時間です。
その都度更新されるようです。
timeは、「1970-01-01 00:00:00」からの経過秒ですかね?
参考:UNIX & Linux コマンド・シェルスクリプト リファレンス > 日付を取得する

使い方(githubより)

概要

z [-h] [-l] [-r] [-t] [regex1 regex2 ... regexn]
z オプション(あれば) 渡す引数(タブ補完でパスが表示される)


オプション

  • h show a brief help message

たぶんヘルプを出す

  • l list only

たぶんリストだけだす

  • r match by rank only

たぶんランク(おそらくアクセス頻度)を出す

  • t match by recent access only

たぶん最近アクセスしたとこを出す

## 適当に移動してみた後
$ z -h
z [-h][-l][-r][-t] args

$ z -r
2          /Users/typosterr/Development/google_appengine_go
7          /Users/typosterr/Downloads
9          /Users/typosterr/Development

$ z -l
12         /Users/typosterr/Development/google_appengine_go
28         /Users/typosterr/Downloads
36         /Users/typosterr/Development

$ z -t
2727       /Users/typosterr/Development
565        /Users/typosterr/Downloads
9          /Users/typosterr/Development/google_appengine_go

使ってみた感じ、出力順は下に行くほど順位が高いようです。

サンプル

$ z foo         cd to most frecent dir matching foo
## fooに最もマッチしたパス

$ z foo bar     cd to most frecent dir matching foo and bar
## fooとbarに最もマッチしたパス

$ z -r foo      cd to highest ranked dir matching foo
## fooにマッチしランクが一番たかいパス

$ z -t foo      cd to most recently accessed dir matching foo
## fooにマッチするなかで一番最近行ったパス

$ z -l foo      list all dirs matching foo (by frecency)
## fooにマッチするパスを表示

#もちろん、zをインストールしてから移動履歴があるとこでないと対象にはならないです。

まとめ


なるほど、もっと調査が必要だ!!
でも、githubがなければシェルのソースなんて見る機会もないし、調査もしなかったと思います。
githubかわいいよgithub

最後までお読みいただき、ありがとうございました。


参考サイト様