David Harman
David Harman

Reputation: 1

Trying to set a prompt to display a specific portion of PWD in ZSH

I am currently using this in ZSH export PS1='${USER}:${PWD}: '

I could use this: export PS1='${USER}:${PWD##*/} \$ '

But I want to see a folder further up in the folder hierarchy, so I would like to use something like this, export PS1='${USER}:${PWD | cut -d '/' -f6}:${PWD##*/} '

But this doesn't work it just shows this: ${USER}:${PWD | cut -d / -f6}:${PWD##*/}

I want it to show a specific folder (at positision f6) and then the current directory.

Any thoughts on how I can do this?

Upvotes: 0

Views: 864

Answers (1)

Gairfowl
Gairfowl

Reputation: 2981

Does this work?:

setopt prompt_subst
export PROMPT='${USER}:${${(s:/:)PWD}[5]}:${PWD:t} '

Some of the pieces:

  • setopt prompt_subst - allows ${...} in prompts to be expanded. You probably have this in your ~/.zshrc already.
  • PROMPT - same effect as PS1.
  • ${(s:/:)PWD} - splits the working directory value in PWD on /s.
  • ${...[5]} - selects the fifth element of that split (which correlates to the sixth field from cut).
  • ${PWD:t} - selects the 'tail' (last) element from the path.

The s parameter expansion flag is documented in the zshexpn man page, along with the t modifier. You may also want to look at the prompt escapes described in the zshmisc man page, and the precmd hook in zshcontrib.

Upvotes: 2

Related Questions