from Hacker News

Cdpath: Easily Navigate Directories in the Terminal

by kuba-orlik on 12/6/23, 8:03 AM with 43 comments

  • by julianeon on 12/7/23, 4:35 AM

    My surprising finding was that its easier to just hardcode abbreviations to directories. The most unimpressive solution technically and yet also the most effective.

    I have abbreviations for about 20 directories, which essentially combine "cd" and the directory path in one string, no spaces.

    It works because I probably spend about 50-80% of my time in those directories, it's not too much to remember, and nothing can be faster than a one-word command abbreviation to that exact directory.

  • by JNRowe on 12/7/23, 2:35 AM

    The article spends a little time talking about the drawbacks of using CDPATH because it will break scripts, but the better answer is simply not to export it. CDPATH is a shell variable, not an environment variable, and should be treated that way.

    Simply use "CDPATH=here:there" without the export, get the same functionality and no additional breakage.

    Plus, on zsh you can use the tied variable cdpath instead of CDPATH(see typeset -T¹). cdpath is an array that accepts all the regular array operations. For bonus points you can even declare it with -U to remove the need to consider duplicates in your config. Other common -- and useful -- tied variables include path/PATH and fpath/FPATH².

    ¹ https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Command...

    ² https://zsh.sourceforge.io/Doc/Release/Parameters.html#Param...

  • by ughitsaaron on 12/7/23, 6:26 AM

    I’ve been using autojump for years to the point of forgetting it’s not out-of-the-box behavior for zsh.

    https://github.com/wting/autojump

  • by ARandomerDude on 12/7/23, 2:20 AM

    For even more power use z

    https://github.com/rupa/z

  • by amelius on 12/7/23, 10:17 AM

    Looking for a tool/shell that combines navigation and history. E.g. it would tell which commands I previously used while at the current directory.
  • by tedunangst on 12/7/23, 3:44 AM

    Whether the shell searches . absent its presence in CDPATH is shell dependent. It may not try . unless explicitly set.
  • by betenoire on 12/7/23, 5:18 AM

    Here is a little fish function I've used for years.

      function p
          set -l argc (count $argv)
          if test $argc -eq 0
              cd ~/projects
              return
          end
          set -l project $argv[1]
          cd ~/projects/$project
          test $argc -eq 1; and return
          set -l file (fzf -q (string join " " $argv[2..-1]))
          test -z $file; and return
          $EDITOR $file
      end
    
    Jumps to my project roots, and even opens my editor with extra args. Completion, too.

      set -l commands (command ls -p ~/projects/ | grep "/" | tr '\n/' ' ')
      complete -c p -f \
          -n "not __fish_seen_subcommand_from $commands" \
          -a $commands
    
    Maybe someone who uses Fish will find this useful
  • by from-nibly on 12/7/23, 3:14 AM

    I have a little bash script/alias that when you call it searchesy source directory for .git folders and lists all the parent directories in an fzf selector, which then cds to your selection.

    I don't go anywhere else in my filesystem often enough to want anything more than that.

  • by philsnow on 12/7/23, 5:47 AM

    > If you put too many directories with common names into CDPATH and export it, it might break your scripts.

    It will also break your shell history.

    I’m probably in the minority but I’m wary of tools like this (asdf’s .tool-versions and similar things like rbenv/pyenv, and especially direnv) that change behavior based on the state of your shell’s working directory.

    I like being able to ^R to a shell command from a month ago and just run it without worrying whence I’m running it.

    I do like that you can directly use shims from asdf/pyenv/etc.

  • by polm23 on 12/7/23, 2:51 AM

    Why not just use symlinks in your home dir? Like: cd ~/blog
  • by yasser_kaddoura on 12/7/23, 9:52 AM

    Just automate the process using tools that use https://en.wikipedia.org/wiki/Frecency like autojump and fasd. No need to put a cognitive load on maintaining something that change over time when it can be easily automated.
  • by thelastknowngod on 12/7/23, 7:44 AM

    I've been using zshmarks or bashmarks for years now. I couldn't get by without it at this point.

    https://github.com/huyng/bashmarks

    https://github.com/jocelynmallon/zshmarks

  • by hiAndrewQuinn on 12/7/23, 7:29 AM

    Very cool! Very helpful if you want to use the CDPATH variable in your shell scripts.

    For my money, I just use fzf's built in Alt+C keybindings, as I outlined here [1] some months ago. It's been a game changer.

    [1]: https://andrew-quinn.me/fzf/

  • by spelufo on 12/7/23, 3:44 PM

    Also, `cd -` to go back to the previous directory. And for git branches `git checkout -`. Handy.
  • by abhinickz on 12/7/23, 10:51 AM

    I use https://github.com/ajeetdsouza/zoxide, which is inspired by z and autojump.