zoxide
is a replacement for cd
that allows fuzzy matching against commonly accessed directories (e.g., cd long
can get you to /home/User/some/long/path
).
Additionally, I use MSYS2 to have the nice utilities from Linux, which comes with a Unix /home
. Therefore, I have symlinked various Windows directories to make it easier to get to:
/c/Users/User/Desktop
->/home/User/Desktop
/c/Users/User/Documents
->/home/User/Documents
/c/Users/User/Downloads
->/home/User/Downloads
- etc.
(/
is actually /c/msys64
, the install directory is used as the fake /
for the purposes of Unix emulation)
Unfortunately, zoxide
seems to store the real path rather than the symlink, which results in my shell prompt displaying the ugly and long /c/**/*
path instead of the nicer and shorter ~/**/*
symlink path. I have therefore written a function to "minify" it.
# fix path
fp() {
local -r win32_home="${"$(cygpath -u "$USERPROFILE")"//\//\\/}"
local -r unix_home="/home/$(whoami)"
local -r valid_dirs=(
'/Desktop'
'/Documents'
'/Downloads'
)
local cmd="$(echo -nE 'print "1ドル0円2ドル0円"' "if /^$win32_home(\\/[^\\/]+)(\\/.*)*/")"
IFS=$'0円' read -r base rest <<< "$(pwd | perl -ne "$cmd")"
if [[ "${valid_dirs[(ie)$base]}" -le ${#valid_dirs} ]]; then
builtin cd "$unix_home$base$rest"
fi
}
Unfortunately my shell is zsh
and shellcheck
doesn't like that. But if I pretend it's Bash, it seems to be fine.
There are probably ways to improve this, especially the Perl that I rely on for matching (I have never even seen Perl before, it came from many hours of experimenting and consulting with various online sources, including ChatGPT - I only know of Perl as the source of the PCRE regex flavor I wish I could use with the various Unix tools instead of their weird flavor).
-
1\$\begingroup\$ "Unfortunately, zoxide seems to store the real path rather than the symlink" : Yes, behavior of symlink on Windows can be confusing. I did some experiments with it some years ago here: dev.to/hakonhagland/… \$\endgroup\$Håkon Hægland– Håkon Hægland2024年08月17日 06:53:36 +00:00Commented Aug 17, 2024 at 6:53
-
1\$\begingroup\$ "Their weird flavour" being the two standard forms of regular expression we've had since long before Perl popped up? \$\endgroup\$Toby Speight– Toby Speight2024年08月23日 13:40:00 +00:00Commented Aug 23, 2024 at 13:40
-
\$\begingroup\$ Perhaps it's because I first learned regex through my CS classes (properly, rather than taking it as magic incantation), and the way they teach regex there is closer to the way Perl does it, hence my preference. \$\endgroup\$404 Name Not Found– 404 Name Not Found2024年08月24日 06:40:50 +00:00Commented Aug 24, 2024 at 6:40
-
\$\begingroup\$ Hopefully those classes told you about the pitfalls of using PCREs (which is why standard Unix tools use BREs or EREs instead). See swtch.com/~rsc/regexp/regexp1.html, for a speed comparison+discussion and see blog.codinghorror.com/… for why doing a lot in 1 regexp is usually a bad idea anyway. \$\endgroup\$Ed Morton– Ed Morton2024年09月08日 22:43:32 +00:00Commented Sep 8, 2024 at 22:43