【ターミナル】入力補完ツールzsh-completionsで快適なzshライフを

もくじ

zshを使う際に入力補完しれくれるzsh-completionsをインストールすれば快適になるのでおすすめです。

環境

Apple M2
macOS Ventura 13.0
zsh

手順

1. インストール実行

まず、Homebrewで zsh-completions をインストールします。

$ brew install zsh-completions

↓ レスポンス

$  brew install zsh-completions
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-completions/manifests/0.35.
######################################################################### 100.0%
==> Fetching zsh-completions
==> Downloading https://ghcr.io/v2/homebrew/core/zsh-completions/blobs/sha256:3a
######################################################################### 100.0%
==> Pouring zsh-completions--0.35.0.all.bottle.tar.gz
==> Caveats
To activate these completions, add the following to your .zshrc:

  if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit
    compinit
  fi

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 these commands:

  chmod go-w '/opt/homebrew/share'
  chmod -R go-w '/opt/homebrew/share/zsh'
==> Summary
🍺  /opt/homebrew/Cellar/zsh-completions/0.35.0: 151 files, 1.4MB
==> Running `brew cleanup zsh-completions`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
$

2. .zshrcに追加

To activate these completions, add the following to your .zshrc:

  if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit
    compinit
  fi

訳「これらの補完を有効にするには、.zshrc に次のコードを追加します。」

とあるので、書いてあるまま.zshrcに追加します。

  if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit
    compinit
  fi

3. zcompdump をリビルド

$  rm -f ~/.zcompdump; compinit
zsh: command not found: compinit

コマンドがないと言われたら以下を実行してから再実行してください。

$  autoload -U compinit

4. 権限を変更

Additionally, if you receive "zsh compinit: insecure directories" warnings when attempting
to load these completions, you may need to run these commands:

  chmod go-w '/opt/homebrew/share'
  chmod -R go-w '/opt/homebrew/share/zsh'

とのことなので、権限を変更します。

$  chmod go-w '/opt/homebrew/share'
$  chmod -R go-w '/opt/homebrew/share/zsh'

5. 設定を反映

$ source ~/.zshrc

これで作業は完了。

補完されるか確認

例えば、新規ファイル作成コマンドtouchで試したいと思います。

toと入力した後Tabキーを押します

$  to
toe         top         tops        topsyscall  topsysproc  touch

すると上記のように候補が出てきます。

そのままuと打ちtouTabキーを押すと下記のようにコマンドが補完されます。

$ touch

このまま作成したいファイル名を入力すれば簡単ですね!

zsh-completionsで時短していきましょう。

参考

macOS の zsh ではこれだけはやっておこう

macOSにzsh-completionをインストールして初期設定をする - setoya-blog

るび魔術師Lv1のるび魔法 【zsh】【compinit】compinitコマンドが無いけどそれは大丈夫