LoginSignup
15
13

More than 1 year has passed since last update.

gitコマンドのtab補完を有効化する方法とは?(brew/zsh)

Last updated at Posted at 2022-05-31

はじめに

gitコマンドのtab補完を有効化する方法について記事を書いていきたいと思います。
いろいろ検証しましたが、こちらの方法が簡単だと思っています。
(brew版のgitコマンドのtab補完方法になります。)

前提

  • macOSを使用予定(macOS Monterey バージョン12.4)
% sw_vers
ProductName:	macOS
ProductVersion:	12.4
BuildVersion:	21F79
%
  • Homebrew導入済み(バージョン 3.4.11)
% brew --version
Homebrew 3.4.11
Homebrew/homebrew-core (git revision 0416e8f4521; last commit 2022-05-30)
Homebrew/homebrew-cask (git revision 9c7cef6f71; last commit 2022-05-30)
%
  • brewを用いてgit導入済み
% which git
/usr/local/bin/git
% git version
git version 2.27.0
%
  • シェルはzshを使用
% which zsh
/bin/zsh
%

作業手順

ディレクトリの準備

zsh補完用のファイルを置くディレクトリを作成します。
(任意の場所で問題なし)

コマンド
mkdir -p ~/.zsh/completions

作成したディレクトリに移動しておきます。

コマンド
cd ~/.zsh/completions

wgetの準備

brewにwgetがインストール済みであることを確認します。

コマンド
brew list | grep wget
実行例
% brew list | grep wget
wget
%

※grepでwgetがヒットしない場合は、「wgetコマンドのインストール」を参考にしてパッケージのインストールを実施します。

wgetコマンドのインストール
  • wgetコマンドのインストール
コマンド
brew install wget
実行例
% brew install wget
Running `brew update --preinstall`...
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> Updated Formulae
Updated 3 formulae.
==> Updated Casks
Updated 1 cask.

==> Downloading https://ghcr.io/v2/homebrew/core/wget/manifests/1.21.3
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/wget/blobs/sha256:aa706c58ae7e9
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sh
######################################################################## 100.0%
==> Pouring wget--1.21.3.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/wget/1.21.3: 89 files, 4.2MB
==> Running `brew cleanup wget`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
%
  • wgetコマンドのインストール完了確認
コマンド
brew list | grep wget
実行例
% brew list | grep wget
wget
%

補完ファイルのダウンロード

git-completion.bashgit-completion.zshと両方のファイルをダウンロードします。
zshに関しては、_gitというファイル名でダウンロードします。
git-completion.bashgit-completion.zshは依存関係

  • wgetにてgit-completion.bashのダウンロード
コマンド
wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
実行例
% wget https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
--2022-05-30 23:25:24--  https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 2606:50c0:8003::154, 2606:50c0:8001::154, 2606:50c0:8000::154, ...
raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8003::154|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 79837 (78K) [text/plain]
`git-completion.bash' に保存中


git-completion.bash 100%[===================>]  77.97K  --.-KB/s 時間 0.02s

2022-05-30 23:25:25 (4.90 MB/s) - `git-completion.bash' へ保存完了 [79837/79837]

%
  • wgetにてgit-completion.zshのダウンロード
コマンド
wget -O _git https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh
実行例
% wget -O _git https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh
--2022-05-30 23:25:35--  https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh
raw.githubusercontent.com (raw.githubusercontent.com) をDNSに問いあわせています... 2606:50c0:8003::154, 2606:50c0:8001::154, 2606:50c0:8000::154, ...
raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8003::154|:443 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 7133 (7.0K) [text/plain]
`_git' に保存中

_git                100%[===================>]   6.97K  --.-KB/s 時間 0.001s

2022-05-30 23:25:35 (10.3 MB/s) - `_git' へ保存完了 [7133/7133]

%
  • ファイル確認
    ダウンロードしたファイルが存在することを確認します。
コマンド
ls -l ~/.zsh/completions
実行例
% ls -l ~/.zsh/completions
total 176
-rw-r--r--  1 ma-sato  staff   7133  5 30 23:25 _git
-rw-r--r--  1 ma-sato  staff  79837  5 30 23:25 git-completion.bash
%

.zshrcの編集

.zshrcを編集して、起動時にgit補完が可能にする設定を行います。
※存在しない場合は、新規で作成。

コマンド
vim ~/.zshrc

vimが使用できない場合は、viを使用します。

~/.zshrcに追記する内容
## Git補完
fpath=(
  ${HOME}/.zsh/completions
  ${fpath}
)
autoload -Uz compinit
compinit

zshの再読み込み

以下のコマンドを実施して、zshの再読み込みを実施します。

コマンド
source ~/.zshrc

※ターミナルの開き直しでもzshの再読み込みが可能

zsh compinit: insecure directoriesの警告が出た場合

以下のような警告が出ることがあります。

警告
Last login: Tue May 31 08:42:04 on ttys000
zsh compinit: insecure directories, run compaudit for list.
Ignore insecure directories and continue [y] or abort compinit [n]? y%
%

「安全でないディレクトリ、リストに対してcompauditを実行します」という内容になります。

このような場合の対応手順は以下になります。

  • 対象のディレクトリを確認
% compaudit
There are insecure directories:
/usr/local/share/zsh/site-functions
/usr/local/share/zsh
%

→2つのディレクトリが存在することを確認

  • /usr/local/share/zshの権限を755に変更
コマンド
chmod 755 /usr/local/share/zsh/
権限変更後(755)
% ls -la /usr/local/share | grep zsh
drwxr-xr-x   3 ma-sato  admin    96  7  4  2020 zsh
%

権限についての説明は割愛とさせて頂きます。

  • 再度、compauditを実行
コマンド
compaudit
実行例
% compaudit
%

→何も表示されないことを確認

さいごに

他にも方法は沢山あると思いますが、今回はgitコマンドのtab補完を有効化する方法の一例をご紹介しました。

参考記事

15
13
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
15
13