Search code examples
gitmacosterminalzshprompt

How to only show current folder and git branch and ~ for home in zsh


Apple changed shell from bash to zsh in its latest OS, so I'm trying to fix my Terminal prompt now :(.

I would like my prompt to only contain:

  • current directory I'm in (without the full path)
  • NO username and computer name
  • current git branch (colored in green)
  • ~ if I'm in the home directory
  • a $ and a space at the end

I used to have this script in my .bash_profile when I was using bash:

# Git branch in prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

export PS1="\[\033[33;1m\]\W\[\033[32m\]\$(parse_git_branch)\[\033[m\]\$ "
export CLICOLOR=1
export LSCOLORS=ExFxBxDxCxegedabagacad
alias ls='ls -GFh'

I renamed .bash_profile into .zprofile, but all this is not working anymore except for the ls part.

How do I make this work again?


Solution

  • So after more googling and looking into a specific part of zsh manual which could be shown by running man zshmisc I managed to fix this. Here is the code for .zprofile:

    # 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
    setopt PROMPT_SUBST
    PROMPT='%1~ %F{green}${vcs_info_msg_0_}%f $ '
    

    %1~ means that only one last trailing component of the current working directory will be shown, and the home directory will be substituted with ~.