How to get ~/foo from /home/user1/foo?
- by Claudius
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.