これは、なにをしたくて書いたもの?
TerraformやVagrantのコマンドでbash補完ができることを全然知らなかったので、メモしておこうかなと。
環境
今回の環境はこちら。
$ terraform version Terraform v1.12.2 on linux_amd64 $ vagrant version Installed Version: 2.4.7 Latest Version: 2.4.7 You're running an up-to-date version of Vagrant!
Terraformでbash補完を有効にする
Terraformではbashやzshを使っている場合にタブ補完を有効にできます。
Terraform CLI Overview / Shell Tab-completion
コマンドはこちら。
$ terraform -install-autocomplete
これで以下のコマンドが$HOME/.bashrc
に追記されます。
complete -C /usr/bin/terraform terraform
bashを再起動するか$HOME/.bashrc
を読み込むと、補完が有効になります。
$ terraform apply env get init metadata plan refresh taint validate console fmt graph login modules providers show test version destroy force-unlock import logout output push state untaint workspace
オプションまでは補完できないようです…。
$HOME/.bashrc
に追記された内容を削除する場合は、以下のコマンドで。
$ terraform -uninstall-autocomplete
Vagrantでbash補完を有効にする
Vagrantでもbashやzshを使っている場合にタブ補完を有効にできます。
Command-Line Interface / Autocompletion
コマンドはこちら。
$ vagrant autocomplete install --bash Autocomplete installed at paths: - $HOME/.bashrc
こんな感じの内容が$HOME/.bashrc
に追記されます。
# >>>> Vagrant command completion (start) . /opt/vagrant/embedded/gems/gems/vagrant-2.4.7/contrib/bash/completion.sh # <<<< Vagrant command completion (end)
ただこれ、Vagrantのバージョンが変わったら再設定ですね…。
こちらもbashを再起動するか$HOME/.bashrc
を読み込むと補完が有効になります。
サブコマンドやオプションも含めて補完できるようです。
$ vagrant box destroy docker-run help login provision reload rsync-auto ssh suspend cloud docker-exec global-status init package push resume share ssh-config up connect docker-logs halt list-commands plugin rdp rsync snapshot status version $ vagrant box add help list outdated prune remove repackage update $ vagrant up --destroy-on-error --install-provider --no-install-provider --no-provision --provider --provision-with default --help --no-destroy-on-error --no-parallel --parallel --provision -h
こちらは$HOME/.bashrc
への追記内容を削除するコマンドはありません。
バージョンが変わったら困りそうだなと思う部分はこうするのもありですが、bashの起動がちょっと遅くなります…。
# >>>> Vagrant command completion (start) #. /opt/vagrant/embedded/gems/gems/vagrant-2.4.7/contrib/bash/completion.sh . /opt/vagrant/embedded/gems/gems/vagrant-$(vagrant version | perl -wn -e 'print $1 if /Installed Version: (.+)/')/contrib/bash/completion.sh # <<<< Vagrant command completion (end)
こんなところでしょうか。