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 -"
Top comments (22)
I use autopushd and popd and
cd -
cd -
goes to the last directory. If you use it repeatedly, it bounced back and forth between two directories A <-> Bautopushd (which is the automatic version of pushd) put the directories that you've gone through on a stack. So say you do:
Your directory stack will be C, B, 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.
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 usecd ..
to accomplish what you just wrote as compared topopd
?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):
Yes, that makes a lot of sense. Thanks for sharing! 👌
I think
popd
pops off of the directory history stack, rather than the directory hierarchy itself; so if you do this:followed by
popd; popd
, you'll end up in/tmp/
once again. I'm guessing thatautopushd
aliasescd
topushd
to make this more automatic; in zshcd
does that by default.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? 🤔
correct, popd uses the pushd history; it does not use the directory hierarchy. Use
cd ..
for that.Cool idea!
I prefer to use
..
to go to the previous directoryMy
.bash_aliases
file contains thisSo I can simply type
..
to go back.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! ✌️
I just tried it out! Really cool. Thanks for sharing!
Glad you liked it. 🙌
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.
You are right. Teaching people about
cd -
could also be a good #OneDevMinute video. I'll make that.Super glad to hear that. Lots of motivation. It should continue :)
You can use
source .zshrc
to update the changes of yourzshrc
file in the same terminal session.Yes, you can. It doesn't work everywhere for everyone. So, I prefer:
Going to be my next tip.
It works but it creates a new instance of the shell process.
Yes, that's the point.
source
method doesn't always work for every OS. Not everyone hasZsh
, Bash, — folks have many different and custom setups. 🙌Love this tip, and love this format. Great post!
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! ✌️
This alias and lots more comes with Oh My Zsh!
medium.com/@equiman/iterm2-oh-my-z...
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. 🙌
Do tell me how it goes on your end?! 🙌