Skip to content

Instantly share code, notes, and snippets.

@drdv
Last active November 24, 2023 14:24
Show Gist options
  • Save drdv/a843a15303e9f0e58b07ff4dde3667c5 to your computer and use it in GitHub Desktop.
Save drdv/a843a15303e9f0e58b07ff4dde3667c5 to your computer and use it in GitHub Desktop.
autojump
(defun j (&optional show-top-candidates)
"Use autojump to navigate to a directory in dired-mode.
Optional argument SHOW-TOP-CANDIDATES, if non-nil, specifies whether to
list the top candidates (as given by `autojump --complete user-input`) in an ido menu,
otherwise directly jump to top candidate.
Tested with autojump v22.5.3."
(interactive "P")
(let* ((autojump-candidates-regex "^.*__.__\\(.*\\)")
(user-input (read-string "autojump to: "))
(autojump-output
(shell-command-to-string
(format
(if show-top-candidates
"autojump --complete %s"
"autojump %s")
user-input)))
(list_paths (remq nil (split-string autojump-output "\n" t)))
(selected-path
(if (= (length list_paths) 1)
(car list_paths)
(progn
(ido-completing-read
"select directory: "
(seq-filter
#'file-exists-p
(remq nil (mapcar
(lambda (str)
(when (string-match autojump-candidates-regex str)
(match-string 1 str)))
list_paths))))))))
(dired selected-path)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment