đŸ Archived View for gemini.omarpolo.com âș post âș enjoying-cdpath.gmi captured on 2022-03-01 at 15:34:43. Gemini links have been rewritten to link to archived content
âŹ ïž Previous capture (2022-01-08)
âĄïž Next capture (2022-06-03)
-=-=-=-=-=-=-
Published: 2020-06-27
Tagged with:
If you use the shell a lot, you may find this advice useful, in case you didn't already knew it. There's an environment variable called CDPATH, used by most shell I know and others cli tools, whose raison d'ĂȘtre is to simplify âcdâ-ing around.
As various environmental variables in UNIX, its value is a list of directories separated by colons, just like PATH. For instance, this is what I currently have in my â~/.kshrcâ:
export CDPATH=.:$HOME/w:/usr/ports
With that in place, no matter where my current working directory is, I can âcd games/godotâ and jump to â/usr/ports/games/godotâ!
A note of warning: â.â (the dot aka your current working directory) should be present in your $CDPATH, otherwise you won't be able to âcdâ into directories not found in your $CDPATH (you can still use âcd ./$somedirâ, but isn't probably what you want).
Since the entry would be too short otherwise, here's some programs that I know respect $CDPATH, and how they behave.
Just as I showed you up there. When you âcdâ into a directory inside your $CDPATH it will print your new current working directory:
$ cd games/godot /usr/ports/games/godot
It will not, however, autocomplete.
It will behave just like ksh.
zsh respects $CDPATH but it doesnât seem to do completions :(
9portsâ rc does not seem to inherit $CDPATH, but you can set it (unsurprisingly) with
cdpath=(. /usr/ports)
in your â~/lib/profileâ. Other versions of rc (I'm talking about the one you get with the rc package on FreeBSD) do inherit it, so double check!
Additionally, rc prints the âpwdâ only if you're âcdâ-ing into something that's not within the current directory. So:
% pwd /home/op % echo $cdpath . /usr/ports % cd bin # won't print /home/op/bin % cd games /usr/ports/games %
set cdpath = (. /usr/ports)
for the rest, behaves exactly like `rc`. I don't really use csh, nor tcsh, so I can't make further comments.
I've installed fish just for this post. It does respect $CDPATH and, unlike other shells, is also able to do proper autocompletion out-of-the-box!
vi will inherit your $CDPATH (but make sure you're exporting it in the environment!). You can also â:set cdpath=âŠâ if you wish. You cannot edit a file like â:e games/godot/Makefileâ and expect vi to open â/usr/ports/games/godot/Makefileâ though, you need first to â:cd games/godotâ and then â:e Makefileâ!
Emacs vanilla âM-x cdâ respects your $CDPATH, you just have to delete the default text in the minibuffer. It also does proper autocompletion! Additionally, eshell respects $CDPATH too! Not every other Emacs packages will, however. For instance, ivy doesn't seem to care about it.
On the other hand, with Emacs you have other ways to quickly jump around.
-- text: CC-BY-SA-4.0; code: public domain unless specified otherwise
For comments, write at < blog at omarpolo dot com > or @op@bsd.network in the fediverse.