‹ BackHN Continuity

Thread

Small programming tricks

681 points · 294 comments · signa11

  1. GNOMES · · focus · HN ↗
    Simple trick I use almost daily for navigating backwards to an exact directory:

    <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;GNOMES&#x2F;6bf65926648e260d8023aebb9ede9573" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;GNOMES&#x2F;6bf65926648e260d8023aebb9ede9...

    I use this often instead of chaining together multiple &#x27;..&#x2F;..&#x27;.

    Also nice for when I use Zoxide to CD into a deep nested directory. I quickly found out unless you manually CD each hop, the different parent directories aren&#x27;t added to your Zoxide DB. (I have come across snippits to recursively CD into all directories in a path to &quot;prepopulate&quot; Zoxide).

    I made this to jump directly from say `&#x2F;foo&#x2F;bar&#x2F;batz&#x2F;abc&#x2F;123` to `&#x2F;foo&#x2F;bar&#x2F;batz`.

    1. exdeejay_ · · focus · HN ↗
      This is a neat and useful trick, and I will be borrowing it. Thanks!
    2. RhysU · · focus · HN ↗
      Abbreviate moving up in the directory hierarchy:

        alias ..=&quot;cd ..&quot;
        alias ...=&quot;cd ..&#x2F;..&quot;
        alias ....=&quot;cd ..&#x2F;..&#x2F;..&quot;
      
      Just keep typing period and hitting enter until you get where you want according to your prompt. I haven&#x27;t found much use for more than four periods. One period would be well-defined but useless.
      1. segfaltnh · · focus · HN ↗
        I have a shell function called &quot;up&quot; that finds a project root and jumps back to it (e.g. a dir containing something like .git)

        Now I have these to complement - thanks!

      2. nandkeypull · · focus · HN ↗
        I used to have these aliases, but then I shortened it even further to a vim-inspired single character which I can spam really fast:

        alias k=&#x27;cd ..&#x27;

    3. zh3 · · focus · HN ↗
      Somewhat along the same lines, as I regularly SSH to other hosts with the same network filesystem mounts, &#x27;scd&#x27; starts an SSH session on a remote host in the same directory.

        #!&#x2F;bin&#x2F;bash
        # SSH to remote system and change into same (current) directory
        DIR=$(pwd)
        ssh -t $1 &quot;cd $DIR; bash -l -i&quot;
      1. bandie91 · · focus · HN ↗
        i made `rr` (remote run) for a use-case very similar to this.

        it expects the remote fs is mounted under ...&#x2F;ssh&#x2F;HOSTNAME&#x2F; and runs any command in that dir on the remote host. if it is not the root which is mounted, looks up mtab for the remote base directory.

        <a href="https:&#x2F;&#x2F;codeberg.org&#x2F;hband&#x2F;hband-tools&#x2F;src&#x2F;branch&#x2F;master&#x2F;user-tools&#x2F;rr" rel="nofollow">https:&#x2F;&#x2F;codeberg.org&#x2F;hband&#x2F;hband-tools&#x2F;src&#x2F;branch&#x2F;master&#x2F;use...

    4. urxvtcd · · focus · HN ↗
      For fish shell users, you get chronological directory traversal with Alt-Left Arrow and Alt-Right Arrow.
    5. pretty_dumm_guy · · focus · HN ↗
      Thank you for mentioning this. Using claude, I created a up.zsh(<a href="https:&#x2F;&#x2F;gist.github.com&#x2F;unbeschwert&#x2F;c2287008734e8034f03e3dd7119a167b" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;unbeschwert&#x2F;c2287008734e8034f03e3dd7...) script and added it to $HOME&#x2F;.oh-my-zsh&#x2F;custom folder.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.