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

Debian Bookworm で Gitコマンドの自動補完を有効化

Last updated at Posted at 2025-01-05

Debian Bookworm で gitのタブ補完が効かない

Debian 12 Bookworm のXfce版で作業中、Gitコマンドのタブ補完が効かないことに気づきました。Fedoraだと問題なく動くのですが...
例えば
git cl[tab]
としても git clone に展開してくれないのです。
ちなみに

$ sudo apt show bash-completion

としてみるとちゃんと bash-completionパッケージは入っていると言われました。

ホームディレクトリ内の.bashrc の内容

ホームディレクトリにある .bashrc を眺めると一番最後に bash-completionを呼んでるらしいスクリプトがちゃんとあるのです。

$ tail ~/.bashrc
# sources /etc/bash.bashrc).
if ! shopt -oq posix; then
  if [ -f /usr/share/bash-completion/bash_completion ]; then
    . /usr/share/bash-completion/bash_completion
  elif [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
  fi
fi

上の . /usr/share/bash-completion/bash_completion でbash_completion スクリプトを呼んでるっぽいなあと思って調べてみたら,

$ ls /usr/share/bash-completion/
completions

あれ? /usr/share/bash-completion/bash_completion なんていうファイルが存在しないのです。
その上 /etc/bash_completion というファイルも存在しませんでした。

ただし bash自体のタブ補完は効いています。例えば his[タブキー] と入力すると history に展開されます。今の所調べた範囲では, どこからどこにある bash補完のスクリプトを呼んでいるのか解明できませんでした。

/usr/share/bash-completion/completions/git

あちこち探しているうちに、/usr/share/bash-completion/completions/git というファイルが git のタブ補完用のスクリプトであることがわかりました。このファイルの最初の方に

# To use these routines:
#
#    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.git-completion.bash

と使い方が明記されてました。

結局

$ cp /usr/share/bash-completion/completions/git ~/.git-completion.bash 

を実行して、gitのタブ補完用のファイルを、ホームディレクトリ直下の .git-completion.bash というファイルにコピーします。
このあとホームディレクトリの .bashrc ファイルの末尾に

. $HOME/.git-completion.bash

という記述を入れて、gitタブ補完ファイルを source コマンド (= 上では ドットコマンド)からホームディレクトリ直下の .git-completion.bash を実行します。これでgit のタブ補完が効くようになりました。

参考文献

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