9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bash でもよく使うディレクトリをブックマークしたい

Last updated at Posted at 2014-03-22

よく使うディレクトリをブックマークする zsh のプラグイン なんていう記事を見たので、bash で似たような事をやってる方法を共有します。

.bashrc にこんなの書くと、ショートカットが定義できます。タブ補完も OK。
bash 初心者なので、突っ込み歓迎です。

declare -A shortDirs
shortDirs=(\
  ["log"]="/var/log"\
  ["loghttpd"]="/var/log/httpd"\
)
goto(){
  for key in ${!shortDirs[*]}; do
    if [ "$key" = "$1" ]; then
      cd ${shortDirs[$key]}
      return 0
    fi
  done
  cd ~
}
gotoParams=""
for key in ${!shortDirs[*]}; do
  gotoParams="$gotoParams $key"
done
complete -W "$gotoParams" goto

この例だと

$ goto l<tab>
で
$ goto log ---> cd /var/log

$ goto l<tab>h<tab>
で
$ goto loghttpd ---> cd /var/log/httpd

になるので、ショートカットの定義次第でいい感じになります。

9
9
4

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
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?