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!
Top comments (40)
pushd
andpopd
are 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
cd
command with the-
character, it jumps back one directory!This only works for a single level; whereas
pushd/popd
will store as many moves as you want, if you issuecd -
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 youcd
to the wrong place, then want to jump back to where you were.Another related protip: the
git checkout
command adopted the same convention, so thatgit checkout -
will checkout out the previous branch!~- 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 doingcd ../../../..
orcd ~
!A side effect of this that sometimes catches me off guard is that if you try to
cd
to an environment variable that doesn't exist, or if you misspell a variable (e.g.cd $PORJ_HOME
instead of$PROJ_HOME
), you end up running plaincd
by accident and going home instead! Always takes me a second to realize what happenedMaybe worthwile to note that
zsh
gives you multiplecd -
entries and you can even iterate and inspect the list when hitting tab. Quite nice. And thenwd
plugin for "warp points" (basicallycd X
aliases, but with convention).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
projects
I'll always go to the~/Documents/projects
folder, but if I typeprojects img
I'll have a good chance of going to the~/Documents/projects/static/img
folder.The alias is using z, but only to change to a folder under
~/Documents/projects
.Otherwise, you could use a better z :)
github.com/ajeetdsouza/zoxide
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 thepushd
set of commands so much, I have them shortened topu
,po
, andds
(fordirs -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 thePATH
variable, you can givecd
a list of places to look for directories you've asked for. You should usually make.
the first directory inCDPATH
, but you can add as many additional directories as you like. So in your examples, if you add~/Documents
to yourCDPATH
, you could get to your projects folder by just typingcd 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
. Thepushd
command should obey theCDPATH
as well, but I haven't tested it in a while.Hello Jimmy,
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. :)
Supercharge your cd command with something like this:
Then in
~/bin/goto
you can put extended logic that will find where you want to go, for exampleFinally, if you prefer to use
cd
rather thangoto
add an extra alias:Example Usage:
For entering frequently visited directories I really enjoy combination of
Then
CTRL-G dev
will 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/popd
for decades, actually, but never thought to include an automaticls
on 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
&& ls
at 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: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!