share
Unix & LinuxCreating a custom tilde-like directory?
[0] [1] p0sa
[2023-07-22 19:59:10]
[ bash terminal prompt ]
[ https://unix.stackexchange.com/questions/752146/creating-a-custom-tilde-like-directory ]

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 ~

If you don't want cd to take you to /home/user anymore you can just overwrite HOME: HOME=~/Desktop/projects/project1 - Arkadiusz Drabczyk
@ArkadiuszDrabczyk problem is if i do that then all the config files also start showing up there - p0sa
so you can create another variable: PROJECT=~/Desktop/projects/project1, or P for short and just cd $P - Arkadiusz Drabczyk
@ArkadiuszDrabczyk Well im not a big fan of having to type the "$" but beside that my purpose is to shorten my prompt and unfortunately it would still show the whole path in my prompt even if its defined as a variable. Unless you know of a way to prevent that? - p0sa
(1) well 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
@ArkadiuszDrabczyk even if define a variable $P=~/Desktop/projects/project1 after i cd $P my prompt would look like this ~/Desktop/projects/project1$. Im referring to the actual bash prompt. - p0sa
(1) You can re-define PS1 using PROMPT_COMMAND to show something different if you are in a specific directory but either your question isn't written clearly or I failed to parse it. - Arkadiusz Drabczyk
@ArkadiuszDrabczyk perhaps i shouldn't have included the backstory. Basically im just asking if its possible to create a custom tilde directory like ~ but #. That has the exact same behavior except for a different directory. - p0sa
As suggested add CDPATH="$HOME/Desktop/projects" to your ~/.bashrc, then you can use e.g. cd project1 to get to ~/Desktop/projects/project1. - Cyrus
(1) For a minute I thought we were looking at Stephen's mkdir $HOME/\~ idea - Jeff Schaller
[+1] [2023-07-23 00:32:46] p0sa

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$ 

1