LoginSignup
303
215

More than 3 years have passed since last update.

【zsh】絶対やるべき!ターミナルでgitのブランチ名を表示&補完【git-prompt / git-completion】

Last updated at Posted at 2020-10-19

:star2: この記事でできること

  • ターミナルのプロンプトにgitのブランチ名を常に表示させる(git-prompt)
  • gitコマンドをTab補完できるようにする(git-completion)

スクリーンショット 2020-10-17 22.36.17.png

:star: はじめに

:computer: 確認環境

  • macOS Catalina 10.15.7

:pencil2: git-prompt / git-prompt の用意

mkdir ~/.zsh
cd ~/.zsh

curl -o git-prompt.sh https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh
curl -o git-completion.bash https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
curl -o _git https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.zsh

:pencil: .zshrc に設定追記

~/.zshrc
# git-promptの読み込み
source ~/.zsh/git-prompt.sh

# git-completionの読み込み
fpath=(~/.zsh $fpath)
zstyle ':completion:*:*:git:*' script ~/.zsh/git-completion.bash
autoload -Uz compinit && compinit

# プロンプトのオプション表示設定
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWSTASHSTATE=true
GIT_PS1_SHOWUPSTREAM=auto

# プロンプトの表示設定(好きなようにカスタマイズ可)
setopt PROMPT_SUBST ; PS1='%F{green}%n@%m%f: %F{cyan}%~%f %F{red}$(__git_ps1 "(%s)")%f
\$ '

:rocket: 以上!

  • より良いgitライフを!
303
215
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
303
215