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
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
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"
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"
.. will take you up a directory, clear the screen and list out all your files in that directory for you. Pretty neat, huh?
Combine the use of chaining with
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!
Top comments (41)
popdare super handy for jumping to several locations, then trying to backtrack to where you were a few jumps ago; I've known about them for a while and still haven't gotten into the habit of using them.
But for simple one-off cases, just jumping to one directory and the jumping back to the last one, there's another option that I use more often: if you call the
cdcommand with the
-character, it jumps back one directory!
This only works for a single level; whereas
pushd/popdwill store as many moves as you want, if you issue
cd -a bunch of times, you'll just jump back and forth between the same two directories. But for most of my own day to day uses, it gets the job done. This is especially handy when you weren't planning to jump back, like when you misremember where something is and you
cdto the wrong place, then want to jump back to where you were.
Another related protip: the
git checkoutcommand adopted the same convention, so that
git checkout -will checkout out the previous branch!
Maybe worthwile to note that
zshgives you multiple
cd -entries and you can even iterate and inspect the list when hitting tab. Quite nice. And then
wdplugin for "warp points" (basically
cd Xaliases, but with convention).
~- is the value of the shell variable
OLDPWD, so you can list out your previous directory with ls ~- . Saves a little time from having to type all the extra characters in cd - && ls && cd - .
But since it holds the actual directory path, it's even more useful because you can quickly list out any subdirs with ls ~-/oldPwdSubDir/anotherSubdir
Another useful feature of
cd, calling it with no arguments takes you to
~. This can save you doing
A side effect of this that sometimes catches me off guard is that if you try to
cdto an environment variable that doesn't exist, or if you misspell a variable (e.g.
cd $PORJ_HOMEinstead of
$PROJ_HOME), you end up running plain
cdby accident and going home instead! Always takes me a second to realize what happened
I ❤️ aliases
You gotta check out rupa/z. It's not quite as reliable as an alias, it is magical. It's based on your frecently used directories.
rupa / z
z - jump around
I was hopping to see this kind of tools at this post
I use a combination of z and aliases. E.g.:
So now, if I just type
projectsI'll always go to the
~/Documents/projectsfolder, but if I type
projects imgI'll have a good chance of going to the
The alias is using z, but only to change to a folder under
Otherwise, you could use a better z :)
One extra of using &&, if the first command fails it stops, if you need to run all commands even when any of them returns an error you can use ;
As && you also can use ||, to execute something if the previous command returns an error, very handy on scripts, something like:
command > /dev/null 2>&1 || exit 1
To suppress stdou and stderr of command but stop the script if returns an error
That's actually really useful to know! Thanks for sharing!
I've never gotten into the habit of using aliases for
cd-ing into directories, but I use the
pushdset of commands so much, I have them shortened to
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
CDPATHvariable. Much like you can set your default binary paths with the
PATHvariable, you can give
cda 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
CDPATH, you could get to your projects folder by just typing
cd projectsfrom wherever you were. As long as the current directory didn't contain a "projects" folder, you'd be taken to your folder at
pushdcommand should obey the
CDPATHas well, but I haven't tested it in a while.
thanks for article.
Please check the code in the CD Aliases section. There are missing the
The correct code should be:
Thank you very much! Good catch. The changes are saved now. :)
For entering frequently visited directories I really enjoy combination of
CTRL-G devwill show me the list of all most used directories with dev in their name sorted by the usage frequency. Hitting enter with enter the directory.
Great idea, Jimmy! I’ve used aliases with
pushd/popdfor decades, actually, but never thought to include an automatic
lson 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.
&& lsat the end of this, though, prevents applying it in general, as any trailing file/directory arguments are picked up by the
ls. 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
.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:
Supercharge your cd command with something like this:
~/bin/gotoyou can put extended logic that will find where you want to go, for example
Finally, if you prefer to use
gotoadd an extra alias:
A small typo in:
alias ..="cd .. && clear && ls"
alias ...="cd ../.. && clear && ls"
alias ....="cd ../../.. && clear && ls"
alias ....="cd ../../../.. && clear && ls"
the 4th alias is identical to the 3rd one, so will over-write it; I imagine you meant the 4th alias to be five dots, not just four like the 3rd one.
Fixed! Thank you!
Good post :)
While I recognize the value in setting up custom aliases, IMHO, fzf (github.com/junegunn/fzf) is all I need. I can type like a raging lunatic monkey and most of the time it still guesses what I actually wanted to type. fzf changed my life!
Make aliases more easily:
This is leet af! I'm stealing this for sure.
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.
Greaaaaaaat!!!! pushd is just amazing! I will miss typing
cdon my terminal from now on. Thanks!
I love aliases, laziness converted to productivity hacks.
Here is my implementation of common git shortcuts, along with some common patterns using bash functions.
Enforce git messages format without throwing everyone off?
Piyush Kumar Baliyan ・ Jul 2 '20 ・ 3 min read
I didn't know you could do git aliases like that. Really cool stuff. Thanks for sharing! 🔥
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.