Reputation: 765
I am trying to display the git branch on prompt on Big Sur.
So I have created a script file to run for each new session .zshrc
# Git branch in prompt.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/'
}
export PS1="\u@\h \W\[\033[01;33m\]\$(parse_git_branch)\[\033[00m\] $ "
The above doesn't work it actually displays the string not its outpupt.
\u@\h \W\[\033[01;33m\]$(parse_git_branch)\[\033[00m\] $
How can I show current branch in prompt on zsh shell?
Upvotes: 12
Views: 13824
Reputation: 61
As for me next is the best for MAC
###
# ADD GIT INFO TO PROMPT
###
# Init color
autoload -U colors && colors
# Git branch info
git_info() {
ref=$(git symbolic-ref HEAD 2> /dev/null) || return
echo "(${ref#refs/heads/})"
}
# Precmd is executed just before each prompt
precmd() {
PS1="%{$fg[blue]%}%~ %{$fg[yellow]%}$(git_info)
%{$fg[green]%}%n@%m%{$restore_color%} %# "
}
Upvotes: 1
Reputation: 16545
I was looking for a script that would do the same on MacOS but didn't find a good answer so I just built it looking up zsh a little bit.
The other answers are right that you the answer is that you're using bash symbols instead of zsh symbols however below is a solution I made.
Add the following to your ~/.zshrc or ~/zprofile
###
# ADD GIT INFO TO PROMPT
###
parse_git_branch() {
local branch=""
branch=$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/')
local git_status=$(git status --porcelain 2>/dev/null)
local color=green
if echo "$git_status" | grep -q "^ M"; then
color=yellow
branch="${branch}*"
fi
if echo "$git_status" | grep -qE "^ A|^\?\?"; then
color=yellow
branch="${branch}+"
fi
if echo "$git_status" | grep -q "^ D"; then
color=yellow
branch="${branch}-"
fi
if [[ -n "$branch" ]]; then
branch=[%F{${color}}${branch}%F{reset}]
fi
echo "$branch"
}
update_prompt() {
PS1="%n %1~$(parse_git_branch) %#"
}
precmd_functions+=(update_prompt)
update_prompt
This will label your branch and give a little mark if there's changes.
If you don't need the mark, only keep the first line in parse_git_branch
If you need the computer name change %n
to %n@%m
.
Remember to re-open the terminal or run source ~/.zshrc
after you make your changes.
Upvotes: 9
Reputation: 766
I am assuming you want to display something like this [username@computername directory](branch)
The code below should do it for Zsh which is a little more involved than accomplishing the same task in bash
. There are multiple solutions to this, and you can find more information here if you are interested.
Add the following to your .zshrc or .zsh_profile
# Load version control information
autoload -Uz vcs_info
precmd() { vcs_info }
# Format the vcs_info_msg_0_ variable
zstyle ':vcs_info:git:*' formats '%b'
# Set up the prompt (with git branch name)
setopt PROMPT_SUBST
PROMPT='[%n@%m %1~]%F{green}(${vcs_info_msg_0_})%F{white}$ '
It would be beneficial to read up on the differences between bash
and zsh
as you cannot use solutions found online interchangeably.
Upvotes: 16
Reputation: 142005
Bash PS1 is not ZSH PS1, they differ. The \u \h
etc. sequences are substituted by bash, they do not work in ZSH. Research both shells and how they differ.
Upvotes: 1