0

I am using macOS Big Sur Version 11.4 and just updated my .zshrc file to show git branch location on my terminal command line.

.zshrc file contains the following. I am not sure what was there before. I accidentally just created .zshrc file using touch ~/.zshrc without checking if the file was already there. Maybe this is where things got messed up.

function parse_git_branch() {
    git branch 2> /dev/null | sed -n -e 's/^\* \(.*\)/[\1]/p'
}

COLOR_DEF=$'\e[0m'
COLOR_USR=$'\e[38;5;243m'
COLOR_DIR=$'\e[38;5;197m'
COLOR_GIT=$'\e[38;5;39m'
setopt PROMPT_SUBST
export PROMPT='${COLOR_USR}%n ${COLOR_DIR}%~ ${COLOR_GIT}$(parse_git_branch)${COLOR_DEF} $ '

The git branch shows, but I noticed when I type over 28 characters on the command line, it disappears like this: this

How can I fix this?


  • Parts of the prompt string that don't move the cursor should be enclosed in `%{ %}` -- see [the manual](https://zsh.sourceforge.io/Doc/Release/Prompt-Expansion.html#Visual-effects) – dave_thompson_085 Jan 08 '22 at 18:51
  • You'll probably have better luck using `vcs_info` for the git branch, and replacing the color escape sequences with the newer `%F` and `%f` expansions. Some more info [here](https://stackoverflow.com/q/69357323/9307265), [here](https://stackoverflow.com/q/70190952/9307265), and [here](https://stackoverflow.com/questions/tagged/colors). – Gairfowl Jan 09 '22 at 10:08
  • BTW, `touch ~.zshrc` didn't really affect anything. If the file already exists, `touch` will only set the modification timestamp of that file; it will not alter the contents. If the file does not exist, then touch will create a new, empty file. – Gairfowl Jan 09 '22 at 10:22

1 Answers1

0

I was able to fix the line wrapping issue with this same .zshrc by wrapping each of the color expressions in the prompt in %{...%} -so for the last line:

export PROMPT='%{${COLOR_USR}%}%n %{${COLOR_DIR}%}%~ %{${COLOR_GIT}%}$(parse_git_branch)%{${COLOR_DEF}%} $'
Ithuvanian
  • 53
  • 5