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

【ターミナル】シンプルにGitの現在のパスとブランチ名を表示する

1
Last updated at Posted at 2026-02-17

どういうことか

  • ターミナルの頭に現在のパスとGitのブランチ名を表示する
  • 手っ取り早く表示させたい人向け
  • bash ではなく zsh に書くのを想定している

設定を書く

vimでもなんでもいいけど ~/.zshrc に以下を書く。

$ vi ~/.zshrc
setopt PROMPT_SUBST

parse_git_branch() {
  local branch
  branch=$(git branch --show-current 2> /dev/null)
  [[ -n "$branch" ]] && echo "[$branch]"
}

export PS1='%n: %~ $(parse_git_branch) %# '

これでシンプルに現在のパスとブランチ名が表示される。
さらに着色などをして見やすくもできる。

setopt PROMPT_SUBST

parse_git_branch() {
  local branch
  branch=$(git branch --show-current 2> /dev/null)
  [[ -n "$branch" ]] && echo "%F{cyan}[$branch]%f"
}

export PS1='%F{green}%n%f: %F{yellow}%~%f %B$(parse_git_branch)%b %# '

ユーザー名をグリーン、パスをイエロー、ブランチ名をシアンにして太字にしている。
色を変えたい場合は %F{色名} の部分を書き換えればよい。使える色名は black red green yellow blue magenta cyan white など。
あとは好みで変えてください。

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