Making custom, single-character directory mappings like ~ > $HOME

We all know that ~ maps to $HOME or /home/user.

Is it possible to make other mappings like this on other characters (@,_)?

A possible workaround I thought of was to use variables (which require at least two characters)...

H=/run/media/user/Storage
cp $H/file.txt .

But is there a better way?


This is possible on a Mac and Linux

On Mac

  • /Users/[yourusername]/.bash_profile

Note: You should also be able to use .profile file, but I do not use it. I just use .bash_profile, because it more specific, like .bashrc on linux, rather than .cshrc.

Instructions

Make a directory mapping (set a variable) to the desktop.

  1. In Terminal.app, run

    nano ~/.bash_profile OR nano $HOME/.bash_profile

  2. Add the following somewhere in your file.

    [nameofvariable]="$HOME/Desktop"

    (for example, pathtodesktop="$HOME/Desktop")

  3. Quit Terminal and restart or run

    source ~/.bash_profile

Finished! Your new variable should function just like $HOME or ~, except to whichever path you choose.

In order to test it you could run:

cd $pathtodesktop

On Linux Just follow the same instructions, except the file you want to edit is under $HOME/.bashrc or $HOME/.bash_profile.


Above and Beyond the Question If you have a command that you frequently run, you could create an alias in your .bash_profile as well like this:

alias dt="cd $HOME/Desktop"

Refresh your terminal paths by restarting terminal or with

source ~/.bash_profile

Type just dt and your path should change to the Desktop