Search code examples
zshrc

Need to display previous folder in the modified zshrc file, how do I do it?


This is my current configuration of the zshrc file for showing git branch which results in showing the current folder with the git branch (if present)

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

setopt PROMPT_SUBST
export PROMPT='%F{cyan}%.%f %F{blue}$(parse_git_branch)%f%F{normal}$%f '

Instead of . in the PROMPT, I tried ~ but then it will show the complete path of the directory, but I wish to only see the current directory and the previous one. How should I proceed?

export PROMPT='%F{cyan}%.%f %F{blue}$(parse_git_branch)%f%F{normal}$%f '

If I am currently in Documents/gitRepos/project1, terminal would show me project1 [master], what I want is gitRepos/project1 [master]


Solution

  • Zsh has this built in:

    %c
    %.
    %C
    

    Trailing component of the current working directory. An integer may folow the '%' to get more than one component. Unless '%C' is used, tilde contraction is performed first. These are deprecated as %c and %C are equivalent to %1~ and %1/., respectively, while explicit positive integers have the same effect as for the latter two sequences.

    So, simply replace %. with %2. or %2~ or whatever you prefer here.