2
\$\begingroup\$

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).

asked Aug 17, 2024 at 4:50
\$\endgroup\$
4
  • 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\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Sep 8, 2024 at 22:43

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.