Get Function Into PS1 (Zsh)?

13,016

Solution 1

You have to include

setopt PROMPT_SUBST

in your .zshrc, man zshall explains it in the PROMPT EXPANSION section:

If the PROMPT_SUBST option is set, the prompt string is first subjected to parameter expansion, command substitution and arithmetic expansion.

Solution 2

Actually your problem was not just setting PROMPT_SUBST: you use double quotes in your script forcing the evaluation of the function when you set the PROMPT variables. You only want evaluation when the prompt is computed that is you must use single quotes.

Solution 3

As akira says, you have to use prompt subst. This is my early code (still working on it):

setopt PROMPT_SUBST
PROMPT='$(parse_git_branch)'

or better

setopt PROMPT_SUBST
PROMPT='[$PR_MAGENTA%n$PR_NO_COLOR@$PR_GREEN%U%m%u$PR_NO_COLOR:$PR_RED%2c$PR_NO_COLOR]$(parse_git_branch) %(!.#.$)'
Share:
13,016
Dan Rosenstark
Author by

Dan Rosenstark

When I was born, I knew nothing about programming. Projects MIDI Designer Pro for iPad, iPhone, iPod touch—dream | create | play your Perfect MIDI Controller on iOS MJDJ—Desktop Java application for MIDI Morphing (transforming) Handsonic Editor—Powerful and popular editor for the Roland Handsonic HPD-15 The KBase—A multi-hierarchical text editor (.Net standalone and Web versions) Technical rambles (blog) Contact Contact me via MIDIdesigner.com

Updated on September 17, 2022

Comments

  • Dan Rosenstark
    Dan Rosenstark over 1 year

    This works in bash (parse_git_branch is a defined function)

    export PS1="\$(parse_git_branch)"
    

    But I cannot figure out the equivalent in zsh.

    Note: If I do

    PROMPT="$(parse_git_branch)"
    

    It seems to work, but in fact it's running the command when I set the prompt, which is not the point.