Overview
Today, I'm going to be sharing a little trick I use to teleport around my terminal with ease.
Do you change to your project directory like this?
> ~
$ cd Documents
> ~/Documents
$ cd projects
> ~/Documents/projects
$ cd my-project
> ~/Documents/projects/my-project
$ ls
src/ package.json yarn.lock etc...
Then this article is for you!
Pushd and Aliases
If you have directories that you commonly go to, it can be incredibly useful to create aliases using pushd.
If you don't know anything about your bashrc, pushd, or setting up aliases, check out my Bash Script Tool Kit blog.
We can try adding something like this to our bashrc:
alias projects="pushd ~/Documents/projects"
alias experiments="pushd ~/Documents/experiments"
Now, anytime we type projects or experiments into our terminal, we can instantly "teleport" to these directories!
But it doesn't stop there, folks! You're probably asking yourself right now, "Jimmy, why use pushd when cd would do the same thing?"
That's a very good question! Pushd has a special feature. After we pushd into a directory we can use popd to pop back to the directory we were previously in. Say we are in our experiments folder and we type in projects into are terminal. Now, if we want to go back to our experiments directory, all we have to do is type popd and we're back were we were!
&&
&& is a very useful and powerful tool when it comes to writing bash alias. It lets us chain commands after one another in one line! So lets use this && operator to chain some extra commands and improve our scripts.
alias projects="pushd ~/Documents/projects && clear && ls"
alias experiments="pushd ~/Documents/experiments && clear && ls"
You can see here that we've chained clear and ls to our commands. So now when we use the projects alias it will take us to our projects directory, clear our screen, and list out all the files and directories. Pretty sweet, huh?
Another alias I use quite a lot is:
alias x="clear && ls"
CD Aliases
How many times do you do this:
$ cd ..
$ ls
Why not set up a couple of aliases that do the hard work for you?
alias ..="cd .. && clear && ls"
alias ...="cd ../.. && clear && ls"
alias ....="cd ../../.. && clear && ls"
alias .....="cd ../../../.. && clear && ls"
Now .. will take you up a directory, clear the screen and list out all your files in that directory for you. Pretty neat, huh?
Conclusion
Combine the use of chaining with && and pushd we can create some incredibly useful alias that help us not only teleport around our filesystem, but keep it clean and organized as well.
What are some of your favorite bash scripts or aliases? I'd love to hear about them in the comments!

Latest comments (40)
Lovely tips, thanks a lot!
Great idea, Jimmy! I’ve used aliases with
pushd/popdfor decades, actually, but never thought to include an automaticlson the end. For example:This uses one other nice feature of pushd — by itself it swaps the top two items on the directory stack! So you can quickly go back and forth between two directories with a single comma.
Including
&& lsat the end of this, though, prevents applying it in general, as any trailing file/directory arguments are picked up by thels. But if you instead use a shell function:it works both with and without an argument.
Also, if you don’t mind occasionally typing out
source, you can repurpose.:I’m using
.here because it’s next to,, it signals finality, and there really aren’t any other punctuation keys available without shift that aren’t interpreted by the shell for something else 😀I really like the
..idea, though I doubt I would use more than one level very often. One other suggestion I would make is to perhaps provide a variant,to add the parent directory to the directory stack.
Finally, sometimes I want to remind myself of the current directory stack, which is displayed with
dirs. Again, being short on punctuation keys but given this is not that common, I can alias it to,., which is in keeping with the rest of these options:More than 10 years ago I wrote github.com/bulletmark/cdhist which I use probably 100 times per day. I don't know how the rest of you get by without it!
These are my aliases and adding yours... Very cool recommendations. Thanks
Hey, instead of typing e or exit, you can do Ctrl-d, it will do the same as exit in almost all cli apps!
Basically Ctrl-d sends a special control character to the application, saying "there is nothing left to read!" which will usually close the cli app.
I've been using autojump for mac/unix for a while now. Just using
j <part of the dir>and it "teleports" you automatically to your last recent places. The only thing needed is to cd at least once to the repository.If you are using ohmyszsh: github.com/ohmyzsh/ohmyzsh/tree/ma...
pj my-projectfor openingmy-projectpjo my-projectfor open the project with yout$EDITORMake aliases more easily:
This is leet af! I'm stealing this for sure.
I have this little function in my
.zshrcwhich is pretty convenient:Had no idea about &&! Great tip. Thanks!
Greaaaaaaat!!!! pushd is just amazing! I will miss typing
cdon my terminal from now on. Thanks!