zsh-completion
最終更新
役に立ちましたか?
zshを使うときに、補完用の情報を設定をしておくことで、例えば、git、aws-cli、dockerなどのコマンドやサブコマンドをTabキーで補完してくれるツールです。
公式サイトは、以下です。
macOSを使っている前提ですが、homebrewで簡単にインストールできます。
% brew install zsh-completion
zsh-completionをインストールすると、以下のようなメッセージがでます。
zsh-completions: stable 0.33.0 (bottled), HEAD
Additional completion definitions for zsh
https://github.com/zsh-users/zsh-completions
/opt/homebrew/Cellar/zsh-completions/0.32.0_1 (142 files, 1.1MB) *
Poured from bottle on 2021-05-03 at 10:22:36
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/zsh-completions.rb
License: MIT-Modern-Variant
==> Options
--HEAD
Install HEAD version
==> Caveats
To activate these completions, add the following to your .zshrc:
autoload -Uz compinit
compinit
You may also need to force rebuild `zcompdump`:
rm -f ~/.zcompdump; compinit
Additionally, if you receive "zsh compinit: insecure directories" warnings when attempting
to load these completions, you may need to run this:
chmod -R go-w '/opt/homebrew/share/zsh'
zsh completions have been installed to:
/opt/homebrew/share/zsh/site-functions
==> Analytics
install: 9,459 (30 days), 39,117 (90 days), 147,856 (365 days)
install-on-request: 9,388 (30 days), 38,843 (90 days), 146,385 (365 days)
build-error: 0 (30 days)
なお、このメッセージは、brew info zsh-completion
コマンドで再表示できます。
このメッセージにしたがって、まずは、次のように .zshrc
ファイル設定します。
# zsh-completionの設定
autoload -Uz compinit
compinit
次に、zsh関連ファイルの権限設定を行います。これも前述のbrew info
で表示されるメッセージに従います。
前述の brew info
で表示されるメッセージの /opt/homebrew
の部分は、使用しているMacのCPUがIntel製かApple Siliconかによって異なります。
なので、環境に応じて次のようにコマンドを打ちます。
% chmod -R go-w /usr/local/share/zsh/
% chmod -R go-w /opt/homebrew/share/zsh/
あるいは、以下のコマンドを打てばどちらの環境でもOKです。
% chmod -R go-w $(brew --prefix)/share/zsh/
なお、この go-w
は、g(roup)とo(thers)から(つまりuser以外から)、書き込み権限を削除(-)しています。
homebrewでgitをインストールすると、自動的に /usr/local/share/zsh/site-functions/ 以下に、git用の補完設定もインストールされるので、それ以上何もする必要はありません。
gitはmacOS標準のものもありますが、上記の補完設定のために、筆者はhomebrewでgitをインストールしなおしてしまいます。
設定がうまくいっている場合、以下のように補完されます。ブランチ名などは、覚えられないので補完を使うと便利になります。
% git branch <TAB>
(ここに候補となるブランチ一覧が表示される)
公式サイトに説明があるので、それに従います。手順は公式サイトに記載されていて、また、迷うところもないので、ここに手順を記載するのは省略します。
設定がうまくいっている場合、以下のような補完が使えます。
% docker container <TAB>
attach -- Attach to a running container
commit -- Create a new image from a container's changes
cp -- Copy files/folders between a container and the local filesystem
create -- Create a new container
diff -- Inspect changes on a container's filesystem
exec -- Run a command in a running container
export -- Export a container's filesystem as a tar archive
inspect -- Display detailed information on one or more containers
kill -- Kill one or more running containers
logs -- Fetch the logs of a container
ls -- List containers
pause -- Pause all processes within one or more containers
port -- List port mappings or a specific mapping for the container
prune -- Remove all stopped containers
rename -- Rename a container
restart -- Restart one or more containers
rm -- Remove one or more containers
run -- Run a command in a new container
start -- Start one or more stopped containers
stats -- Display a live stream of container(s) resource usage statistics
stop -- Stop one or more running containers
top -- Display the running processes of a container
unpause -- Unpause all processes within one or more containers
update -- Update configuration of one or more containers
wait -- Block until one or more containers stop, then print their exit codes
aws-cliも公式サイトに従うだけですが、少しわかりにくいので、補足説明を追加します。
公式サイトは、以下です。
なお、この記事では、AWS CLI の記事に記載さた手順でインストールされている前提で補足説明をします。
前述の前提の場合、aws_completer はすでにPATHが通っているので、やることは以下の設定を.zshrc
ファイルに追加するだけです。
autoload bashcompinit && bashcompinit # この行を追加
autoload -Uz compinit
compinit
complete -C '/usr/local/bin/aws_completer' aws # この行を追加
設定後は、zshを再起動します。
設定がうまくいっていれば、以下のようにawsコマンドの補完が効くようになります。
% aws s<TAB>
s3 securityhub sqs
s3api serverlessrepo ssm
s3control service-quotas sso
s3outposts servicecatalog sso-admin
sagemaker servicecatalog-appregistry sso-oidc
sagemaker-a2i-runtime servicediscovery stepfunctions
sagemaker-edge ses storagegateway
sagemaker-featurestore-runtime sesv2 sts
sagemaker-runtime shield support
savingsplans signer swf
schemas sms synthetics
sdb snowball
secretsmanager sns
本サイトの更新情報は、Twitterの株式会社プレセナ・ストラテジック・パートナーズエンジニア公式アカウントで発信しています。ご確認ください。