8

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?

Sergey Zakharov
  • 1,493
  • 3
  • 21
  • 40
  • You should take a look at the sections EXPANSION OF PROMPT SEQUENCES and SIMPLE PROMPT ESCAPES in `zshall(1)`, plus the `vcs` extension for Git. – bk2204 Nov 23 '19 at 16:16
  • @bk2204 but what is `zshall(1)`? How do I look at its sections? – Sergey Zakharov Nov 23 '19 at 17:08
  • It's a manual page. You'd invoke `man zshall`. – bk2204 Nov 23 '19 at 18:33
  • Apple changed the *default* shell to `zsh`; you can switch back to `bash` if you like. In the future, I suspect Apple will stop shipping a version of `bash` with the operating system, but even then, you should be able to install `bash` on your own and use that as your default shell. – chepner Nov 23 '19 at 22:55
  • @chepner I know I can switch back to `bash`, but I don't want that. – Sergey Zakharov Nov 24 '19 at 00:12
  • 1
    The prompt stuff is specifically available from `man zshmisc`; `man zshall` gives you *all* the documentation, which is a lot to sort through. – chepner Nov 24 '19 at 01:02
  • Just as a casual nod you might want to look at https://ohmyz.sh/ because it's got lots of canned prompts and themes. – the Tin Man Nov 24 '19 at 01:54

2 Answers2

10

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

Sergey Zakharov
  • 1,493
  • 3
  • 21
  • 40
  • If you want to remove the "double space" at the end of the prompt for non-git folders, you can change `zstyle ':vcs_info:git:*' formats '%b'` to `zstyle ':vcs_info:git:*' formats '%b '` (add extra space infront of 'b') ... and change change `PROMPT='%1~ %F{green}${vcs_info_msg_0_}%f $ '` to `PROMPT='%1~ %F{green}${vcs_info_msg_0_}%f$ '` (remove extra space before of '$') – markhops Sep 29 '20 at 14:49
2

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_} $ '
Felix Geenen
  • 2,465
  • 1
  • 28
  • 37