#OneDevMinute: Go back to the Previous Directory

Ahmad Awais ⚡️ on September 20, 2018

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. 🔥 Lite... [Read Full]
markdown guide
 

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.

 

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?

 

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

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:

$ 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.

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 directory

My .bash_aliases file contains this

alias ..="cd .."

So 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 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.

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

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

 
 

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! ✌️

 

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

 

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.

 

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 has Zsh, Bash, — folks have many different and custom setups. 🙌

 
 
 

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. 🙌

code of conduct - report abuse