Horacio
Horacio

Reputation: 1964

How to get absolute path on a zsh prompt?

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) '

Upvotes: 26

Views: 38346

Answers (4)

Nera Zython
Nera Zython

Reputation: 1

export PROMPT=${PROMPT//\%c/\%d}

Just append above code into .zshrc and source again.

Because %c behalf of name and %d is full path.

Upvotes: 0

atsu85
atsu85

Reputation: 541

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

Upvotes: 30

fend25
fend25

Reputation: 163

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:)

Upvotes: 16

Caleb Adams
Caleb Adams

Reputation: 4905

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 %/

Upvotes: 15

Related Questions