#OneDevMinute: Go back to the Previous Directory
Ahmad Awais ⚡️

Ahmad Awais ⚡️ @ahmadawais

About: VP of DevRel RapidAPI ❯ Award-winning Web Developer NodeCLI.com ❯ Google Dev Expert Web tech ❯ 2x GitHub Stars Award ❯ WordPress Core Dev ❯ TEDx Speaker ❯ "awesome example for devs" — Satya Nadella

Location:
San Francisco Bay Area
Joined:
Feb 19, 2017

#OneDevMinute: Go back to the Previous Directory

Publish Date: Sep 20 '18
19 22

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

Comments 22 total

  • Ahmad Awais ⚡️
    Ahmad Awais ⚡️Sep 20, 2018

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

  • Muhammad Osama Arshad
    Muhammad Osama ArshadSep 20, 2018

    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.

    • Ahmad Awais ⚡️
      Ahmad Awais ⚡️Sep 20, 2018

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

  • Ben Halpern
    Ben HalpernSep 20, 2018

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

    • Ahmad Awais ⚡️
      Ahmad Awais ⚡️Sep 20, 2018

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

  • Valentin Baca
    Valentin BacaSep 20, 2018

    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.

    • Ahmad Awais ⚡️
      Ahmad Awais ⚡️Sep 20, 2018

      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?

      • Rob Hoelz
        Rob HoelzSep 20, 2018

        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.

        • Ahmad Awais ⚡️
          Ahmad Awais ⚡️Sep 20, 2018

          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? 🤔

          • Valentin Baca
            Valentin BacaSep 20, 2018

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

      • Valentin Baca
        Valentin BacaSep 20, 2018

        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
        
  • Camilo Martinez
    Camilo MartinezSep 21, 2018

    This alias and lots more comes with Oh My Zsh!

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

    • Ahmad Awais ⚡️
      Ahmad Awais ⚡️Sep 21, 2018

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

  • Maqbool
    MaqboolSep 21, 2018

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

    • Ahmad Awais ⚡️
      Ahmad Awais ⚡️Sep 21, 2018

      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.

      • Maqbool
        MaqboolSep 21, 2018

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

        • Ahmad Awais ⚡️
          Ahmad Awais ⚡️Sep 21, 2018

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

  • Ben Sinclair
    Ben SinclairSep 22, 2018

    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.

    • Ahmad Awais ⚡️
      Ahmad Awais ⚡️Sep 22, 2018

      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 :)

Add comment