34

Bash has the PROMPT_DIRTRIM option, e.g. when I set PROMPT_DIRTRIM=3, then a long path like:

user@computer: /this/is/some/silly/path

would show instead as:

user@computer: .../some/silly/path

Does a similar option exist for zsh?

techraf
  • 5,831
  • 10
  • 33
  • 51
pfnuesel
  • 5,702
  • 8
  • 35
  • 60

4 Answers4

46

To get a similar effect like in bash, that is including the ..., try using:

%(4~|.../%3~|%~)

in in your PROMPT variable (which might be also named PS1 in your configuration) place of %~. This checks, if the path is at least 4 elements long (%(4~|true|false)) and, if true, prints some dots with the last 3 elements (.../%3~), otherwise the full path is printed (%~).


I noticed that bash seems to shorten paths in the home directory differently, for example:

~/.../some/long/path

For a similar effect, you may want to use:

%(5~|%-1~/…/%3~|%4~)

This checks, whether the path is at least 5 elements long, and in that case prints the first element (%-1~), some dots (/…/) and the last 3 elements. It is not exactly the same as paths, that are not in your home directory, will also have the first element at the beginning, while bash just prints dots in that case. So

/this/…/some/silly/path

instead of

.../some/silly/path

But this might not necessarily a bad thing.


Instead of %~ you can also use %d (or your current PROMPT might already use %d). The difference is, that %d shows full absolute paths, while %~ shows shorthands for “named directories”: e.g. /home/youruser becomes ~ and /home/otheruser becomes ~otheruser. If you prefer to use the full path as basis for the shortening, just replace any occurrence of ~ with d.

Adaephon
  • 4,356
  • 1
  • 25
  • 28
  • 1
    I like the first version better, but note that it shows `~/a/b` as `.../a/b` instead of `~/a/b`. To fix that, you can use `%(5~|…/%3~|%~)`. – momar Apr 22 '16 at 11:17
  • 1
    For those new to zsh (like me), you want to use this syntax with the `PROMPT` environment variable in your `.zprofile`, e.g.: `EXPORT PROMPT="%(5~|%-1~/…/%3~|%4~)"` – Ryan H. Jun 06 '19 at 14:28
  • 2
    @RyanH. Thanks, reading my answer again I notice, that I never mentioned, where these settings need to be made. I will have to fix that. Two small things about your comment: settings for interactive shell sessions should be made in `.zshrc` and there is really no need to `export` any shell internal parameters like `PROMPT`. – Adaephon Jun 06 '19 at 14:38
  • @Adaephon Thanks for the info. I will fix! :) – Ryan H. Jun 06 '19 at 14:47
  • 1
    For what it’s worth, this `%(4~|.../%3~|%~)` actually checks whether the path is longer than **or equal to** four elements. – TransferOrbit Nov 18 '21 at 20:41
  • Where do you put `%(5~|…/%3~|%~)`? Directly putting it in `.zshrc` doesn't seem to work – ru111 Aug 19 '22 at 14:08
15

In addition to the other answers given here, you can also use %< to truncate the path to a given number of characters. I find this preferable to using %<n>d, since individual path elements may obviously be quite long in themselves. Using %< yields a far more predictable maximum prompt length.

For example, to left-truncate the path element with tilde expansion (%~) to 15 characters, replacing removed characters with .., you can do something like this:

PROMPT='%n@%m:%15<..<%~%<<%# '

This is documented in the Zsh manual under Prompt Expansion, right at the end of the page.

wjv
  • 1,201
  • 10
  • 8
5

See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html or 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 ‘~’.

So to get something similar to PROMPT_DIRTRIM=3, you could use %3d or %3~, e.g.

% mkdir -p ~/a/b/c/d
% cd ~/a/b/c/d
% PS1='%n@%m: %3d%% '
user@computer: b/c/d% 
Mikel
  • 56,387
  • 13
  • 130
  • 149
4

You can use %3d prompt expansion:

/home/cuonglm/.config/fish/functions $ PS1='%3d $ '
.config/fish/functions $

The general form is %d, if any positive integer follow d specifies the trailing components to show of current path, zero mean show the whole path, negative integer means the leading path to show:

.config/fish/functions $ PS1='%-2d $ '
/home/cuonglm $
cuonglm
  • 150,973
  • 38
  • 327
  • 406