A bit of backstory: For a while now i've just been putting my personal files & directories in the user home directory. However something that has always annoyed be about this is that they get mixed and intertwined with all the various system, config, and hidden, files from applications.
I think its impossible to prevent the clutter of config files in the home directory so i decided to start using my Desktop directory instead. I mean after all this is its purpose.
Problem is now my prompts are quite long, as they went from ~/projects/project1$ to ~/Desktop/projects/project1$, which i am not a fan of.
I was wondering if there was a way to create a new "~" tilde-like directory in bash specifically for my Desktop directory? A simple symlink unfortunately wont suffice as id want to be able to type cd # anywhere and it should show up in my prompt as #/projects/project1$ just as the behavior of ~
So i didn't quite figure out how to really create a custom tilde-like directory (nor if its even possible) however i figured out how one could kind of fake it, and that works fine for my purposes.
Firstly in your .bashrc create a wrapper for cd that substitutes your special character for your directory (in this example i use ^):
#cd wrapper that substitutes ^ for ~/Desktop in the same way ~ works for $HOME
cd()
{
local dir=''
local input="${1}/" # forces / at the end of path used for substitution later
case "${input}" in
(^/*)
dir="$HOME/Desktop/${input#*/}" ;;
(*)
command cd "$@"
return
;;
esac
command cd "${dir}"
}
Then secondly you want to modify your prompt so that it shows the special character in place of $HOME/Desktop, this is done by setting PROMPT_COMMAND to a function that gets called every time the prompt is shown, in which it just does the substitution manually (note it also takes care of ~ as that wouldn't happen otherwise) and inserts it into the PS1 variable:
#manually insert working directory in the prompt so i can substitute $HOME/Desktop with ^
update_ps1()
{
# Get the current working directory
local current_dir=$(pwd)
if [[ $current_dir == $HOME/Desktop* ]]; then
substituted_dir="${current_dir/#$HOME\/Desktop/^}"
else
substituted_dir="${current_dir/#$HOME/\~}"
fi
PS1="\u@\h:$substituted_dir\$ "
}
PROMPT_COMMAND=update_ps1
Note: Its probably also possible to get autocomplete to work by substituting it in a autocomplete function, however i dont need this for my personal purposes.
Anyways this achieves what i was looking for:
user@computer:~$ cd ^
user@computer:^$ pwd
/home/user/Desktop
user@computer:^$ cd projects
user@computer:^/projects$
cdto take you to /home/user anymore you can just overwrite HOME:HOME=~/Desktop/projects/project1- Arkadiusz DrabczykPROJECT=~/Desktop/projects/project1, orPfor short and justcd $P- Arkadiusz Drabczykwell my purpose is to shorten my prompt and unfortunately with a variable it still shows the whole path- I don't get it, you would just type$P, not the full path. Another idea is to use CDPATH. - Arkadiusz Drabczyk$P=~/Desktop/projects/project1after icd $Pmy prompt would look like this~/Desktop/projects/project1$. Im referring to the actual bash prompt. - p0saCDPATH="$HOME/Desktop/projects"to your~/.bashrc, then you can use e.g.cd project1to get to~/Desktop/projects/project1. - Cyrus