The Bash prompt supports the
\w escape sequence, documented as
\w the current working directory, with $HOME abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)
Is there any way to get a similar abbreviation for an arbitrary string? That is, is there a general command that does something like the following, provided that HOME=/home/user1
/home/user1 → ~ /home/user1/a/1 → ~/a/1 /home/user2/b/2 → ~user2/b/2 /root → ~root
Sure, I could try something ugly with sed, but that is unlikely to give me the result I want in any case. :-)
The movitation behind this is that I would like to keep the titles in the tabs of my terminals as short as possible, hence abbreviate working directories where possible.
UPDATE: As there doesn’t appear to be a satisfactory solution easily available, and I would like to keep things sufficiently fast, I now went with a simple
pwd | sed -e "s|/root|~root|" -e "s|/home/user1|~|
and something similar for root. As I am the only user on the machine, this currently works more or less. I will keep the question open in case someone comes up with
magical-tool-that-solves-all-problems (ideally not written in elisp…).