I'm switching from bash to zsh.
I want to update my new zsh prompt and looked around to find a way, but I have only found "solutions" via oh-my-zsh.
The goal:
Location: ~/dir_1/dir_1_1/dir_1_1_1
What I have:
Location: dir_1_1_1
The code (source):
PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[green]%}Location: %c%{$reset_color%}$(git_prompt_info) '
Look for %c on the second line. That's the part of the prompt we're trying to change and currently it represents just the working directory. Change it to %~ . That's all there is to it, you'll now get the full path.
/home/dave/dir and ~/dir are both absolute paths, the second uses an abbreviation for your home directory. A relative path is a path that is relative to your current directory (e.g. ../dir ) rather than starting at root ( / ). p.s. Nice use of color to indicate exit status of previous command.
Prompt sequences undergo a special form of expansion. This type of expansion is also available using the -P option to the print builtin. If the PROMPT_SUBST option is set, the prompt string is first subjected to parameter expansion, command substitution and arithmetic expansion.
The default prompt for zsh is: phoenix% echo $PROMPT %m%# The %m stands for the short form of the current hostname, and the %# stands for a % or a # , depending on whether the shell is running as root or not. zsh supports many other control sequences in the PROMPT variable.
To preserve original prompt format (colors, git info and potentially other customisations before this one) except related to path info, you could append following to the end of ~/.zshrc:
PROMPT=${PROMPT/\%c/\%~}
As pointed out by @caleb-adams and @fend25 the key is replacing
%c
(just folder name) with%~
to include full path (or absolute from $HOME when under ~). See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html for more info
As Horacio Chavez mentioned in the comment above, you want to look here: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html for the details on how to change your displayed path in zsh.
In this case if you are looking for a path that is relative to your home folder, include a %~
in your zsh-theme file. Your prompt would now look like this:
PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[green]%}Location: %~%{$reset_color%}$(git_prompt_info) '
note, I only changed one character in your prompt. the %c
was swapped for the %~
. %c
will only give your current directory (see the document link above, or here)
For a full path you could use %/
Simplest way to add bash-style dir path to the prompt. Just add this to ~/.zshrc
:
setopt PROMPT_SUBST
PROMPT='%n@%m: ${(%):-%~} '
The part with the path is ${(%):-%~}
. Colouring could be added according with your lifestyle:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With