DEV Community

Play Button Pause Button
Ahmad Awais ⚡️
Ahmad Awais ⚡️

Posted on • Updated on

#OneDevMinute: Go back to the Previous Directory

My first video post here. Go easy on me.

Easily go back the previous directory you accessed in the terminal with this simple bash alias.

🔥 Literally the best dev tip of the day, I use it all the time.

⌨️ Type - to go back to prev directory

🤔 HOW?
✅ Add this alias to your .bashrc/.zshrc files
♻️ Reload your terminal

alias -- -="cd -"
Enter fullscreen mode Exit fullscreen mode

Top comments (22)

Collapse
 
val_baca profile image
Valentin Baca

I use autopushd and popd and cd -

alias po=popd

cd - goes to the last directory. If you use it repeatedly, it bounced back and forth between two directories A <-> B

autopushd (which is the automatic version of pushd) put the directories that you've gone through on a stack. So say you do:

cd A
cd B
cd C

Your directory stack will be C, B, A.

popd # now we're in B
popd # now we're in A

They do similar, but different things and depending on what you're doing one will be useful where the other isn't.

I find that while working through source code autopushd and popd are what I prefer as I'm crawling through code.

cd - is very useful when you are looking in say the code path, and want to hope between the logs directory.

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

Cool beans! 👌

I forget which one but a Linux flavor didn't play well with popd maybe it was Windows Subsystem Linux. Anywho, popd is a different thing then what this tip is about. Did you know you can also use cd .. to accomplish what you just wrote as compared to popd?

Collapse
 
val_baca profile image
Valentin Baca

popd is different. I'm sorry if it wasn't clear that I meant "this is a similar, but different tool". I did not mean for it to be "that's bad do this instead."

cd .. goes up a directory in the hierarchy. popd goes back in the pushd history.

If you're going "down" in folders, then yes they would happen to do the same thing. But if you're moving across folders, they'll do different things.

Here's a comparison (pretend autopushd is on):

cd /usr/bin
cd /tmp/logs
cd .. # will take you to /tmp
cd .. # will take you to /
cd /usr/bin
cd /tmp/logs
popd # will take you to /usr/bin
popd # will take you to where you were before the first command
Thread Thread
 
ahmadawais profile image
Ahmad Awais ⚡️

Yes, that makes a lot of sense. Thanks for sharing! 👌

Collapse
 
hoelzro profile image
Rob Hoelz

I think popd pops off of the directory history stack, rather than the directory hierarchy itself; so if you do this:

$ pushd /tmp
$ pushd
$ pushd projects/

followed by popd; popd, you'll end up in /tmp/ once again. I'm guessing that autopushd aliases cd to pushd to make this more automatic; in zsh cd does that by default.

Thread Thread
 
ahmadawais profile image
Ahmad Awais ⚡️

Makes a lot of sense. And I think typing

-

to go to prev dir is quite easy for me to remember. Wouldn't you agree? 🤔

Thread Thread
 
val_baca profile image
Valentin Baca

correct, popd uses the pushd history; it does not use the directory hierarchy. Use cd .. for that.

Collapse
 
asamolion profile image
Muhammad Osama Arshad • Edited

Cool idea!

I prefer to use .. to go to the previous directory

My .bash_aliases file contains this

alias ..="cd .."

So I can simply type .. to go back.

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️ • Edited

Well, I think you didn't watch the video. .. takes you one directory up and not in the previously opened directory. cd - takes you to the previous directory what ever it was. Try it to understand it or watch the video :)

Peace! ✌️

Collapse
 
asamolion profile image
Muhammad Osama Arshad

I just tried it out! Really cool. Thanks for sharing!

Thread Thread
 
ahmadawais profile image
Ahmad Awais ⚡️

Glad you liked it. 🙌

Collapse
 
moopet profile image
Ben Sinclair

I don't understand why you bother with an alias. If you spent the minute explaining what cd - did that'd be enough, and it would equip users with the ability to flip between directories on any system, not just the one they have the alias set up on.

I really like your one-dev-minute thing, by the way.

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

You are right. Teaching people about cd - could also be a good #OneDevMinute video. I'll make that.

I really like your one-dev-minute thing, by the way.

Super glad to hear that. Lots of motivation. It should continue :)

Collapse
 
maqbool profile image
Maqbool

You can use source .zshrc to update the changes of your zshrc file in the same terminal session.

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

Yes, you can. It doesn't work everywhere for everyone. So, I prefer:

# Reload the shell (i.e. invoke as a login shell)
alias reload="exec $SHELL -l"

Going to be my next tip.

Collapse
 
maqbool profile image
Maqbool

It works but it creates a new instance of the shell process.

Thread Thread
 
ahmadawais profile image
Ahmad Awais ⚡️

Yes, that's the point. source method doesn't always work for every OS. Not everyone has Zsh, Bash, — folks have many different and custom setups. 🙌

Collapse
 
ben profile image
Ben Halpern

Love this tip, and love this format. Great post!

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

That's very kind of you to say! 🙌
I am planning to make more of these posts. I am calling this a new #DevTip in one minute. There'll be more.

P.S. I wish the tweets would refresh the new stats. Looks like when you embed a Tweet it stays that way, the likes and retweets are never refreshed. 🤔

P.P.S. I really want the code styling to have 🦄 Shades of Purple styles. Is that possible? I think you folks are using HighlighJS? I have a theme for that github.com/ahmadawais/Shades-of-Pu... —— Can it happen?

Looking forward, peace! ✌️

Collapse
 
equiman profile image
Camilo Martinez • Edited

This alias and lots more comes with Oh My Zsh!

medium.com/@equiman/iterm2-oh-my-z...

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️ • Edited

YES YES YES. As a core contributor of OhMyZsh I already know that :) But not everyone wants to use all of the aliases in it. That's why I am sharing this.

Also, it's a #OneDevMinute series where I share one development tip in one minute. I hope you appreciate the effort behind this. 🙌

Collapse
 
ahmadawais profile image
Ahmad Awais ⚡️

Do tell me how it goes on your end?! 🙌