Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute path on a zsh prompt?

Tags:

zsh

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) '
like image 309
Horacio Avatar asked Jan 06 '16 00:01

Horacio


People also ask

How do you get the full path on zsh?

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.

How can I display the absolute path in bash prompt?

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

What is Prompt_subst?

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.

What is zsh prompt?

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.


3 Answers

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

like image 193
atsu85 Avatar answered Oct 22 '22 20:10

atsu85


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

like image 45
Caleb Adams Avatar answered Oct 22 '22 21:10

Caleb Adams


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

like image 12
fend25 Avatar answered Oct 22 '22 20:10

fend25