Hacker News new | ask | show | jobs
by ForkMeOnTinder 913 days ago

  alias ..='cd ..'
  alias ...='cd ../..'
  alias ....='cd ../../..'
  alias .....='cd ../../../..'
  alias ......='cd ../../../../..'
  alias .......='cd ../../../../../..'
Merry christmas, HN!
7 comments

I used to do things like this until I found bash has

shopt -s autocd

which makes it so naming a directory cd's to it, including .. ~ etc.

I think it's a terrible idea, because one day you'll try to enter a directory that shares its name with a command you don't know, and it will execute this command instead
up() { cd $(eval printf '../'%.0s {1..$1}); }

So you can just type something like `up 2` to move by two directories.

eval printf? um.... is your name little bobby tables by any chance?
I devised `cdb` for this: https://github.com/jafarlihi/dotfiles/blob/a5133f49d67732199...

You can do just `cdb 5` to navigate 5 directories back.

Not bash but if you're using fish you can use https://github.com/nickeb96/puffer-fish
I did not realize cd'ing up was an esoteric sport. My own take:

  shopt -s cdable_vars
  u2=../.. u3=../../.. u4=../../../.. u5=../../../../..
I use numbers to avoid counting how many dots I am writing, just use 1. 2. 3. 4. to call these cd 1, 2, 3, or 4 levels up.
my version of ..

  function ..() {
    local temp_dir=$PWD
    cd ..
    for token in "$@"; do
      cd ..
    done
    OLDPWD=$temp_dir
  }
so you can just type .. to go up once or .. [word] to go up twice and so on
That seems potentially unpredictable versus building the string and calling cd once. For example, if you use direnv-type tools there may be side-effects along the route¹. zsh users could have state changing via the chpwd functions too, although that could be mitigated with "cd -q" at least.

Obviously, it might not be an issue for your use, but just noting a potential gotcha if I were to lift it for example.

[ Golf-y zsh, because vacation time and AoC has finished: ..() { cd ../${(pj:/:)${@/*/..}} } ]

¹ even just expensive setup/teardown delays would feel odd*