DEV Community

Jimmy McBride
Jimmy McBride Subscriber

Posted on • Edited on

Bash: How To Teleport In The Terminal

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... 
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Now, anytime we type projects or experiments into our terminal, we can instantly "teleport" to these directories!

great Scott

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"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

CD Aliases

How many times do you do this:

$ cd ..

$ ls
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
kmacedovarela profile image
Karina Macedo Varela

Lovely tips, thanks a lot!

Collapse
 
andyanderson profile image
Andy Anderson

Great idea, Jimmy! I’ve used aliases with pushd/popd for decades, actually, but never thought to include an automatic ls on the end. For example:

alias ,=pushd

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 the ls. But if you instead use a shell function:

function , { pushd $1 && clear && ls; }

it works both with and without an argument.

Also, if you don’t mind occasionally typing out source, you can repurpose . :

alias .=popd

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,

alias ,,="pushd .. && clear && ls"

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:

alias ,.=dirs
Collapse
 
bulletmark profile image
Mark Blakeney

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!

Collapse
 
gabrielizalo profile image
Gabriel Porras

These are my aliases and adding yours... Very cool recommendations. Thanks

alias la="ls -lart"  or  la="ls -lrt"
alias e="exit"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bew profile image
Benoit de Chezelles

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.

Collapse
 
mallowigi profile image
Elior Boukhobza

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.

Collapse
 
mateus_vahl profile image
Mateus Vahl

If you are using ohmyszsh: github.com/ohmyzsh/ohmyzsh/tree/ma...
pj my-project for opening my-project
pjo my-project for open the project with yout $EDITOR

Collapse
 
teraspora profile image
John Lynch

Make aliases more easily:

#!/bin/bash
# Quickly make a persistent alias
echo "alias $1='$2'" >> ~/.bash_aliases
. ~/.bash_aliases
Collapse
 
jimmymcbride profile image
Jimmy McBride

This is leet af! I'm stealing this for sure.

Collapse
 
stephencweiss profile image
Stephen Charles Weiss

I have this little function in my .zshrc which is pretty convenient:

# up is a composable version of 'cd ..' taking a numeric argument. if one is passed (e.g., `up 3`, the result is `cd ../../../`)
# taken from here: https://news.ycombinator.com/item?id=9869231
function up {
        if [[ "$#" < 1 ]] ; then
            cd ..
        else
            CDSTR=""
            for i in {1..$1} ; do
                CDSTR="../$CDSTR"
            done
            cd $CDSTR
        fi
    }
Collapse
 
cbloss profile image
cbloss

Had no idea about &&! Great tip. Thanks!

Collapse
 
antoniogamiz profile image
Antonio

Greaaaaaaat!!!! pushd is just amazing! I will miss typing cd on my terminal from now on. Thanks!