LoginSignup
9
11

More than 5 years have passed since last update.

Macのターミナル上でディレクトリ移動が楽になるzshコマンド

Last updated at Posted at 2016-06-14

Macのターミナル上で前のディレクトリに戻るためには下記のようにコマンドを入力する必要があります。

$ cd ../../../

この入力方法は戻るディレクトリの数を数える必要もあり、かなりストレスが溜まります。

そんな悩めるディレクトリ移動を楽にしてくれるzshコマンドがあります。
それがbdコマンドです。

bdコマンドを利用すると以下のように、ディレクトリ名を指定するだけで一発で目的のディレクトリに戻ることが出来るようになります。

Tarrasch/zsh-bd

animation.gif

antigenのインストール

bdコマンドをインストールするために、zshのプラグインマネージャーantigenを利用します。
(「antigenのインストールなんて面倒だ!」という人は、ここを読み飛ばして下記にあるantigenを利用しない場合のインストール方法を参照してください。)

antigenのインストールはgitリポジトリをクローンするだけです。

$ git clone https://github.com/zsh-users/antigen.git ~/.zsh/antigen/

.zshrcの設定

参考にさせて頂いたブログの記事のやり方が非常にスマートだったのでそちらに従って設定を記述していきます。
antigenの設定を他のファイルに分離するために.zshrcにはantigenの設定ファイルの読み込みだけを記述します。

# antigenの設定ファイルを読み込む
source ~/.zshrc.antigen

antigenの設定

antigenの設定は~/.zshrc.antigenという設定ファイルを作成し、そこに記述するようにします。

$ touch ~/.zshrc.antigen

公式のREADME.mdに書かれている設定の例を基に設定ファイルを記述した例です。

# antigen.zshの読み込み
source ~/.zsh/antigen/antigen.zsh

# Load the oh-my-zsh's library.
antigen use oh-my-zsh

# oh-my-zsh上にあるプラグインのインストール 
# Bundles from the default repo (robbyrussell's oh-my-zsh).
antigen bundle git
antigen bundle heroku
antigen bundle pip
antigen bundle lein
antigen bundle command-not-found

# oh-my-zsh以外のzshプラグインのインストール
# Syntax highlighting bundle.
antigen bundle zsh-users/zsh-syntax-highlighting

# Load the theme.
antigen theme robbyrussell

# Tell antigen that you're done.
antigen apply

bdコマンドのインストール

上記で作成した~/.zshrc.antigenを以下のように記述します。

# antigen.zshの読み込み
source ~/.zsh/antigen/antigen.zsh

# zsh-bdのインストール
antigen bundle Tarrasch/zsh-bd

ターミナルを再起動するか、以下のコマンドでzsh-bdがインストールされます。

$ source ~/.zshrc

antigenを利用しない場合のインストール方法

ターミナル上で以下のコマンドを実行して、ターミナルを再起動するか~/.zshrcを読み込みます。

$ mkdir -p $HOME/.zsh/plugins/bd
$ curl https://raw.githubusercontent.com/Tarrasch/zsh-bd/master/bd.zsh > $HOME/.zsh/plugins/bd/bd.zsh
$ print -- "\n# zsh-bd\n. \$HOME/.zsh/plugins/bd/bd.zsh" >> $HOME/.zshrc

参考

zshのプラグインマネージャーantigenでoh-my-zshの良いとこ取りをする
http://blog.glidenote.com/blog/2012/10/25/antigen/

9
11
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
9
11