vim has a really nice feature which it utilizes in its paths when they’re a bit long:

It abbreviates the path to the document in the tab at the top. Is there a way to do something similar to this so my bash prompt doesn’t look like this:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a8dacec3dac7cbc3dcc3e8dfc7dac385c4c9d8dcc7d8">[email protected]</a> ~/Documents/Projects/Work/maventest/src/main/java/com/tkassembled/ $
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I like PROMPT_DIRTRIM in bash…
export PROMPT_DIRTRIM=2
will change your example prompt to…
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcdd9d4cdd0dcd4cbd4ffc8d0cdd492d3decfcbd0cf">[email protected]</a> ../com/tkassembled/ $
It works for me.
Method 2
Try this:
PROMPT_COMMAND='PS1X=$(perl -pl0 -e "s|^${HOME}|~|;s|([^/])[^/]*/|$""1/|g" <<<${PWD})'
or, pure bash:
PROMPT_COMMAND='PS1X=$(p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf "${q:1}")'
then
PS1='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="374277">[email protected]</a>h ${PS1X} $ '
produces (notice the ~ for ${HOME}):
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1664707d6479757d627d566179647d3b7a7766627966">[email protected]</a> ~/D/P/W/m/s/m/j/c/tkassembled $
I improved my answer thanks to @enzotib’s
Method 3
Similar to @nicerobot answer, but somewhat shorter:
PROMPT_COMMAND='pwd2=$(sed "s:([^/])[^/]*/:1/:g" <<<$PWD)' PS1='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="592c19">[email protected]</a>h:$pwd2$ '
This will show the followin example output:
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="35505b4f5a415c577554565047">[email protected]</a>:/h/enzotib$ cd <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7a2a9bda8b3aea587a6a4a2b5">[email protected]</a>:/h/enzotib$ cd /usr/share/doc/acpid/examples/ <a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e3868d998c978a81a382808691">[email protected]</a>:/u/s/d/a/examples$
Method 4
Adding on to enzotib‘s answer, the following snippet will:
- Convert
/Path/To/Your/Home/to~/(so a path will be~/a/b/cinstead of/P/T/Y/H/a/b/c) -
Use the first letter of a ‘dot file’ instead of showing only the dot (
/a/./c/dwill become/a/.b/c/d):PROMPT_COMMAND='PS1_PATH=$(sed "s:([^/.])[^/]*/:1/:g" <<< ${PWD/#$HOME/~})' export PS1='<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6b1e2b">[email protected]</a>h:$PS1_PATH$ '
Make sure you use single quotes or bash will expand it prematurely.
Method 5
The format for use in your PS1 is W (see the PROMPTING section in the bash man page).
PS1="<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e396a3">[email protected]</a>h W$ "
And you might want to read the other options there, you can do some cool stuff with your command line.
Method 6
I love the output from nicerobot’s example but I found one issue. I have a directory that has a hyphen in it, A-E, and it was seeing -E as an argument to printf when it was my current working directory. To fix this, I added -- to the last printf and added quotes around the output in case there is nothing to output:
p="${PWD#${HOME}}"; [ "${PWD}" != "${p}" ] && printf "~";IFS=/; for q in ${p:1}; do printf /${q:0:1}; done; printf -- "${q:1}"
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0