如何在 zsh 提示符下获取绝对路径?

How to get absolute path on a zsh prompt?

我正在从 bash 切换到 zsh。

我想更新我的新 zsh 提示并四处寻找方法,但我只通过 oh-my-zsh 找到了 "solutions"。

目标:

Location: ~/dir_1/dir_1_1/dir_1_1_1

我有:

Location: dir_1_1_1

代码(source):

 PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[green]%}Location: %c%{$reset_color%}$(git_prompt_info) '

As Horacio Chavez mentioned in the comment above, you want to look here: http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html 关于如何在 zsh 中更改显示路径的详细信息。

在这种情况下,如果您正在寻找相对于您的主文件夹的路径,请在您的 zsh-theme 文件中包含一个 %~。您的提示现在看起来像这样:

PS1='${SSH_CONNECTION+"%{$fg_bold[green]%}%n@%m:"}%{$fg_bold[green]%}Location: %~%{$reset_color%}$(git_prompt_info) '

请注意,我只更改了您提示中的一个字符。 %c 被替换为 %~%c只会给你当前目录(see the document link above, or here)

对于完整路径,您可以使用 %/

将 bash 样式的目录路径添加到提示的最简单方法。只需将此添加到 ~/.zshrc:

setopt PROMPT_SUBST
PROMPT='%n@%m: ${(%):-%~} '

带路径的部分是${(%):-%~}。可以根据您的生活方式添加颜色:)

要保留原始提示格式(颜色、git 信息和可能在此之前的其他自定义设置)除了与路径信息相关的内容外,您可以将以下内容附加到末尾~/.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