How to get absolute path on a zsh prompt?
Asked Answered
H

4

24

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) '
Hedve answered 6/1, 2016 at 0:57 Comment(2)
dir_1/dir_1_1/dir_1_1_1 is not full path. Not sure what you want, but most people use %~ or %n~ instead of %c, where n is the number of trailing components to show. %c is deprecated anyway. Read zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html.Grackle
Thanks @Grackle that is exactly what I was looking for.Hedve
K
15

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

Killer answered 31/1, 2016 at 18:53 Comment(0)
A
28

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

Aludel answered 4/6, 2020 at 19:59 Comment(2)
This didn't work for me, on zsh v5.8.1. I added this to my .zshrc and then re-sourced it, but the prompt is still not showing the full path.Mayhew
This also straightforwardly does not work for me on zsh 5.9Hang
M
16

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

Multivocal answered 6/1, 2020 at 15:49 Comment(0)
K
15

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

Killer answered 31/1, 2016 at 18:53 Comment(0)
V
0
export PROMPT=${PROMPT//\%c/\%d}

Just append above code into .zshrc and source again.

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

Valenta answered 20/9, 2023 at 2:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.