Saturday, April 20, 2024
6
rated 0 times [  6] [ 0]  / answers: 1 / hits: 760  / 2 Years ago, mon, december 13, 2021, 1:47:32

I want something like this:



"vivek@grishma:~/xxx/yyy/zzz/src$" to be shown as



"vivek@grishma:datasource$" where I would somehow have predefined "datasource" to be an alias for the long path above.



using the alias command as



" alias datasource='~/xxx/yyy/zzz/src'"



is useful for navigation but it does not take out the long path in the prompt.



Is this possible?



PS- I dont want it to be just "vivek@grishma:" as then each time I should run pwd to know my working directory.


More From » command-line

 Answers
1

This will do:



PS1='u@h:$(
case $PWD in
$HOME/xxx/yyy/zzz/src) echo "datasource ;;
*) echo "w" ;;
esac
)$'


That gives you the flexibility to define other special directories as well.



Aliases won't help you here.



To reduce duplication, put all your special dirs in an array, and use that to generate your aliases and also your prompt. Put all this in your ~/.bashrc:



declare -A labels=(
[$HOME/xxx/yyy/zzz/src]=datasource
[$HOME/foo/bar]=baz
)
for path in "${!labels[@]}"; do
alias "${labels[$path]}"="$path"
done
function path_label () {
local IFS=:
if [[ ":${!labels[*]}:" == *:"$PWD":* ]]; then
# we're in a "known" dir
echo "${labels[$PWD]}"
else
return 1
fi
}
PS1='u@h:$( path_label || echo "w" )$'

[#26256] Tuesday, December 14, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ionash

Total Points: 214
Total Questions: 111
Total Answers: 116

Location: Sudan
Member since Thu, May 7, 2020
4 Years ago
;