grokking hard

code smarter, not harder

Goes back quicker in Terminal

Posted at — 2021-Mar-08

When I was working on a Terminal, I used to type lots of cd and cd .. to go back and forth the directory. Too often, when I went to deep into the directory hierarchy, I got to type multiple times cd .. just to move up to parents, or a long command like cd ../../../../../.

I got frustrated every freaking time doing this, so I thought of an more easy way to type once and it got me to the correct directory up in the hierarchy, I call the alias two-dots.

1
2
3
# Add this into your ~/.bashrc or ~/.zshrc
function back() { for x in {1..${1:-1}}; do cd ..; done }
alias ..='back'

How it works:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# Suppose your are in
$> cd some/project/that/has/a/very/deep/directory/structure

structure $>

# Say you need to go back to 'that' directory.
# Normally, it requires 6 times typing "cd .." to achieve this.
$> .. 6

that $>

If you want to more enhanced version of back() such that typing .. 6 or .. that will produce the same result, you can check out my Gist: