Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 818
Sergey Zakharov Avatar asked Dec 31 '22 11:12

Sergey Zakharov


2 Answers

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 ~.

like image 195
Sergey Zakharov Avatar answered Jan 02 '23 23:01

Sergey Zakharov


This is my .zshrc based on Sergey's great answer. It enhances it by adding some more color and colon separated branch names (only if available).

This also works smooth for the integrated terminals in JetBrains IDE's (IntelliJ / PhpStorm / WebStorm).

enter image description here

# load version control information
autoload -Uz vcs_info
precmd() { vcs_info }

# format vcs_info variable
zstyle ':vcs_info:git:*' formats ':%F{green}%b%f'

# set up the prompt
setopt PROMPT_SUBST
PROMPT='%F{blue}%1~%f${vcs_info_msg_0_} $ '
like image 30
Felix Geenen Avatar answered Jan 03 '23 00:01

Felix Geenen