Bash有PROMPT_DIRTRIM选项,例如当我设置时PROMPT_DIRTRIM=3,然后是一条长路径,如:
user@computer: /this/is/some/silly/path
Run Code Online (Sandbox Code Playgroud)
将显示为:
user@computer: .../some/silly/path
Run Code Online (Sandbox Code Playgroud)
是否存在类似的选项zsh?
Ada*_*hon 38
要获得类似 中的效果bash,即包括...,请尝试:
%(4~|.../%3~|%~)
Run Code Online (Sandbox Code Playgroud)
这会检查路径是否至少有 4 个元素长 ( %(4~|true|false)),如果为真,则使用最后 3 个元素 ( .../%3~)打印一些点,否则打印完整路径%~。
我注意到这bash似乎以不同的方式缩短了主目录中的路径,例如:
~/.../some/long/path
Run Code Online (Sandbox Code Playgroud)
对于类似的效果,您可能需要使用:
%(5~|%-1~/…/%3~|%4~)
Run Code Online (Sandbox Code Playgroud)
这会检查路径是否超过 5 个元素,在这种情况下,打印第一个元素 ( %-1~)、一些点 ( /…/) 和最后 3 个元素。它与路径不完全相同,路径不在您的主目录中,也将以第一个元素开头,而bash在这种情况下只打印点。所以
/this/…/some/silly/path
Run Code Online (Sandbox Code Playgroud)
代替
.../some/silly/path
Run Code Online (Sandbox Code Playgroud)
但这不一定是坏事。
wjv*_*wjv 13
除了此处给出的其他答案外,您还可以使用%<to truncate the path to a given number of characters。我发现这比使用 更可取%<n>d,因为单个路径元素本身可能很长。使用会%<产生更可预测的最大提示长度。
例如,要将带有波浪号扩展 ( %~)的路径元素左截断为 15 个字符,用 替换已删除的字符..,您可以执行以下操作:
PROMPT='%n@%m:%15<..<%~%<<%# '
Run Code Online (Sandbox Code Playgroud)
这在 Zsh 手册中的Prompt Expansion下有记录,就在页面的末尾。
请参阅http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html或man zshmisc
%d
%/
Current working directory. If an integer follows the ‘%’, it specifies a number of trailing components of the current working directory to show
%~
As %d and %/, but if the current working directory starts with $HOME, that part is replaced by a ‘~’.
Run Code Online (Sandbox Code Playgroud)
所以要得到类似的东西PROMPT_DIRTRIM=3,你可以使用%3d或%3~,例如
% mkdir -p ~/a/b/c/d
% cd ~/a/b/c/d
% PS1='%n@%m: %3d%% '
user@computer: b/c/d%
Run Code Online (Sandbox Code Playgroud)