DEV Community

Discussion on: Bash: How To Teleport In The Terminal

Collapse
 
bloodgain profile image
Cliff

I've never gotten into the habit of using aliases for cd-ing into directories, but I use the pushd set of commands so much, I have them shortened to pu, po, and ds (for dirs -v). I don't personally have a huge use for location aliases, as I just use soft links to create shortcuts to them from my home directory. Typing ~/ isn't a huge deal to me.

That said, the "proper" or POSIX-y way to do this is to use the CDPATH variable. Much like you can set your default binary paths with the PATH variable, you can give cd a list of places to look for directories you've asked for. You should usually make . the first directory in CDPATH, but you can add as many additional directories as you like. So in your examples, if you add ~/Documents to your CDPATH, you could get to your projects folder by just typing cd projects from wherever you were. As long as the current directory didn't contain a "projects" folder, you'd be taken to your folder at ~/Documents/projects. The pushd command should obey the CDPATH as well, but I haven't tested it in a while.