1
0

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 3 years have passed since last update.

【Zsh】特定のディレクトリに移動した時にaliasを設定する

Last updated at Posted at 2022-07-16

課題

複数のGoプロジェクトで異なるバージョンのGoを使用している。

記事と関係ないけど複数の異なるGoバージョンをインストールする方法はこちら↓
https://go.dev/doc/manage-install#installing-multiple

これで例えば go1.11.13 みたいなバージョン付きコマンドでGoを実行できる様になるが、出来れば各プロジェクト内では go というコマンドで実行したい。

解決策

そこで、各プロジェクトのディレクトリに移動した際に自動でエイリアスを設定する。
.zshrcにディレクトリ移動する度に実行されるhook関数を記述する。

function aliasgo {
	case $PWD/ in
		~/some-project/*) alias go=~/go/bin/go1.11.13;;
		*)
			if alias go &> /dev/null
			then
				unalias go
			fi
			;;
	esac
}
chpwd_functions+=(aliasgo)

ディレクトリから外れるとunaliasする様にしたけどイマイチ感ある(´・ω・`)
もっとスマートな方法あったら教えて欲しい。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?