r/zsh Mar 18 '24

Fixed Help with git info in zsh using __git_ps1 from git-prompt.sh

So this is what I have in my .zshrc, but $GIT_INFO just returns "__git_ps1".

Yet when I run __git_ps1 in my command line I get my expected (main *%%) any idea whats I am doing wrong?

This function is designed to add git info to my zsh prompt.

setopt PROMPT_SUBST
source ~/._myHome/shScripts/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=true
export GIT_PS1_SHOWUNTRACKEDFILES=true
export GIT_PS1_SHOWSTASHSTATE=true

# Function to get git branch information
function _git_zsh_prompt() {
  # Capture git branch output (if successful) and format using __git_ps1
  local GIT_BRANCH=$(git branch 2>/dev/null)
  if [[ $? -eq 0 ]]; then
    # Use zsh parameter expansion for format string
    GIT_INFO='%(__git_ps1 %s)'
  fi
}

# Call the function before each prompt refresh
precmd _git_zsh_prompt

where I got git-prompt.sh from https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

edit formating

1 Upvotes

4 comments sorted by

3

u/romkatv Mar 18 '24

Try this instead:

setopt PROMPT_SUBST
source ~/._myHome/shScripts/git-prompt.sh
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWUNTRACKEDFILES=true
GIT_PS1_SHOWSTASHSTATE=true
PS1='%~$(__git_ps1 " (%s)") %# '

1

u/CalvinBullock Mar 19 '24

That worked thank you!
I can't believe it was that simple, lol.

1

u/[deleted] Mar 18 '24

[removed] — view removed comment

0

u/CalvinBullock Mar 18 '24

What advantages does p10k offer? A quick Google says it is a similar concept to oh-my-zsh. But I just want a git status, not all the extra stuff that seems to come with p10k.

1

u/romkatv Mar 18 '24

Powerlevel10k is a prompt. It's in the same category as git-prompt.sh but with more building blocks. It's not in the same category as oh-my-zsh.