Saturday, September 16, 2023

Augment With Zoxide

Augmenting Emacs With ZOxide For Efficient File System Navigation

1. Background

Emacs has no shortage of multiple built-in means of navigating the file system with smart, context-sensitive and fuzzy completion. That said, there is one tool outside Emacs I have discovered in the last six months that brings something extra — Zoxide, a smarter cd built in Rust.

2. Default Usage

Once installed, zoxide works well in Emacs shells, as well as at terminals inside or outside Emacs. Read the zoxide docs for more details, but in a nutshell, this tool remembers directories you work in, and lets you jump to them by typing short, unique substrings.

3. Working In Emacs

So with zoxide installed, you can:

  1. Switch to a shell buffer,
  2. Execute a zoxide navigation command, e.g., z <pattern>.
  3. Once there, you can easily open files, launch dired etc.

    But given that opening dired on that target is what I often want, the above work-flow still involved two steps too many. So in typical Emacs fashion, I wrote a short function that short-circuits this process

4. Command: emacspeak-zoxide

Note: there is nothing emacspeak specific in what follows.

  1. Interactive command emacspeak-zoxide prompts for a pattern, then launches dired on the zoxide result.
  2. It uses if-let to advantage:
    • If zoxide is installed,and
    • There is a zoxide result for the specified query,
    • Launch dired on that directory.
    • Else, signal the appropriate error.
    • Notice that if-let expresses this clearly.
(defun emacspeak-zoxide (q)
  "Query zoxide  and launch dired.
Shell Utility zoxide --- implemented in Rust --- lets you jump to
directories that are used often.
This command does for Emacs, what zoxide does at the  shell."
  (interactive "sZoxide:")
  (if-let
      ((zoxide (executable-find "zoxide"))
       (target
        (with-temp-buffer
          (if (= 0 (call-process zoxide nil t nil "query" q))
              (string-trim (buffer-string))))))
      (funcall-interactively #'dired  target)
    (unless zoxide (error "Install zoxide"))
    (unless target (error "No Match"))))

In my setup, i bind this to C-; j which is convenient to press and is a mnemonic for jump.