6
2

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.

fishでもdotを打った分だけ上の階層に行く方法

Posted at

このエントリは,以下のエントリのfish版になります.

zshでdotを打った分だけ上の階層に行くもう一つの方法

私もzshを使っていた際には,cdで複数階層上に移動するのを簡単にするため

alias -g ...='../..'
alias -g ....='../../..'

のようなグローバルエイリアスを設定していました.

fishでもこれと同様のことをしたかったのですが

  • fishにそもそもグローバルエイリアスがない

ことに加え,仮に...を解釈可能なようにcdを再定義1しても

  • ...それ自体は無効なパスなので,補完が効かない
  • fishは標準でシンタックスハイライト有効なので...がエラーとして色付けされてしまう2

という問題がありました.
しかし元エントリを見て,この方法ならfishでも上手くいく! と気付き,さっそく真似してみました.

replace_multiple_dots.fish
function replace_multiple_dots
  set -l token (string match '*..' -- (commandline -tc))
  if test -d "$token"
    commandline -i '/..'
  else
    commandline -i '.'
  end
end

まず上記の関数を定義.その後fish_user_key_bindings関数内に以下を追加します.

fish_user_key_bindings
bind '.' replace_multiple_dots

ちょっとアレンジした点として,入力中の語が実在するパスでない場合は,普通に連続した.が打てるようになっています.

cd .. # <- ここで.を打つとcd ../..に展開
echo "...." # <- パスの一部でないときは普通に.が連打できる

これでfishでも,簡単に階層移動できるようになりました.

  1. ...を解釈可能なように再定義されたcdがoh-my-fishのプラグイン(oh-my-fish/plugin-cd)として存在します.

  2. 別に実害はないのですが,エラーの色付けされているコマンドが実行できてしまうのがなんとなく気持ち悪かったです.

6
2
2

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
6
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?