How can I shorten the bash prompt's current path to one letter per directory?
After playing with this for a while I got the answer you require:
Add this to your .bashrc file in your home directory, exit the terminal and renter it and you will get you prompt.
PS1='$(eval "sps")$ '
sps() {
echo "$PWD" | sed -r 's|/([^/]{,2})[^/]*|/\1|g'
}
It uses the declared function sps()
to evaluate the path every time the variable PS1
which is the prompt, is displayed
ie
/ho/de/De/Ap/Ti$ pwd
/home/deth/Desktop/Apps/Tivo
/ho/de/De/Ap/Ti$
Or...if you insist on the one letter
PS1='$(eval "sps")$ '
sps() {
echo "$PWD" | sed -r 's|/(.)[^/]*|/\1|g'
}
Which displays:
/h/d/D/A/T$ pwd
/home/deth/Desktop/Apps/Tivo
/h/d/D/A/T$
To truncate all directory names except the last one:
PS1='$(eval "sps")$ '
sps() {
python -c "import sys; dirs = sys.argv[1].split('/'); print '/'.join(d[:1] for d in dirs[:-1]) + '/' + dirs[-1]" $PWD
}