[发布] zlua.el: 为 Emacs 带来智能的目录跳转体验

这是一个为 Emacs 打造的 z.lua 集成插件。如果你在终端中使用过 z、autojump 或 fasd,你一定知道它们能极大提高目录切换的效率。z.lua 是其中性能最强、功能最丰富的实现之一。现在,你可以将这种“飞一般”的跳转体验带入 Emacs 了!

:rocket: 核心功能

  1. 极速跳转: 使用模糊匹配快速跳转到常用目录。
  2. 智能学习: 基于“频率”和“时效性”(Frecent)算法,自动学习你的访问习惯。
  3. Dired 集成: 自动跟踪你在 Dired 中访问过的目录,无需手动维护。
  4. 交互式选择: 当有多个匹配结果时,提供交互式界面供你选择。
  5. 文件搜索: 直接在匹配的目录中打开文件,或在所有历史目录中搜索文件。
  6. Shell 共享: 与终端共享同一个数据库,在 Shell 里访问过的目录,Emacs 里也能秒跳,反之亦然。

:video_game: 主要命令

  1. zlua-jump (别名 z)
  • 功能: 快速跳转到最匹配的目录。
  • 用法: M-x z RET foo → 跳到含 “foo” 的最高频目录。
  1. zlua-jump-interactive
  • 功能: 交互式选择跳转目录。
  • 用法: M-x zlua-jump-interactive RET foo → 列出所有含 “foo” 的目录供选择。
  1. zlua-search-dir
  • 功能: 跳到匹配目录并直接选文件。
  • 用法: M-x zlua-search-dir RET project → 跳到 project 目录并打开文件选择器。
  1. zlua-search-file
  • 功能: 在所有历史目录中搜索文件。
  • 用法: M-x zlua-search-file RET config → 查找所有历史目录中含 “config” 的文件。
12 个赞

我用的 https://github.com/emacsmirror/zoxide , 还可以。

(defun my-zoxide-cd-to-scratch ()
  "Use zoxide to select a directory and cd into it in *scratch* buffer."
  (interactive)
  (let ((paths (zoxide-query)))  ; get all paths from zoxide
	(when paths
	  (let ((selected-dir (completing-read "Go to directory: " paths nil t)))
		(when (file-directory-p selected-dir)
		  ;; Apply cd in *scratch*
		  (with-current-buffer (get-buffer-create "*scratch*")
			(cd selected-dir))
		  ;; Also apply to current buffer if it's not *scratch*
		  (unless (equal (buffer-name) "*scratch*")
			(cd selected-dir))
		  (message "Working directory set to: %s" selected-dir))))))

平常基本只用上面这个来改变默认的根目录。

1 个赞

我使用内置的 project-switch-project 命令。主要是如果要切换目录,一般也是在有 .git 目录的 project 之间切换,那么 project-switch-project 配合 vertico 就完全够用了。

1 个赞

z.lua 是根据目录关键词、访问频率、访问新鲜度等因素来综合学习用户习惯,从而根据模糊的关键词就可以智能跳转到用户希望进入的目录。而且和终端共享一个记录文件,只要的是访问的目录就会被记录下来。我用下来感觉很爽,基本上无脑敲几个字母就能直接跳转过去,极大的减少心智负担。

维度 策略
时间敏感度 分段衰减:24小时(4x) > 7天(2x) > 长期(1x)
频率敏感度 访问次数直接叠加(隐含在多次访问记录中)
路径权重 深层目录 > 浅层目录,basename 优先匹配
匹配灵活性 子序列模糊匹配 + 多关键词组合 + 通配符支持
数据驱动 完全依赖历史访问数据(路径+时间戳),无文件系统实时查询
3 个赞

和楼上一样一般我只用内置的 project 相关的

1 个赞

不错,回头试试

看起来很棒哦, 不过我是选择在consult里面集成了zlua, 更简单一点, 只需要传入zlua script脚本的path就行了

(use-package consult-dir
    :init
    (setq consult-dir-default-command #'consult-dir-dired)
    :config
    (defvar consult-dir--source-zlua
      `(:name     "Zlua Dir"
        :narrow   ?z
        :category file
        :face     consult-file
        :history  file-name-history
        :enabled  ,(lambda () (getenv "ZLUA_SCRIPT"))
        :items
        ,(lambda ()
           (nreverse (mapcar
                      (lambda (p) (abbreviate-file-name (file-name-as-directory p)))
                      ;; REQUIRE export `ZLUA_SCRIPT' in parent-shell
                      (split-string (shell-command-to-string
                                     "lua $ZLUA_SCRIPT -l | perl -lane 'print $F[1]'")
                                    "\n" t)))))
      "Zlua directory source for `consult-dir'.")
    (add-to-list 'consult-dir-sources 'consult-dir--source-zlua t))
2 个赞

consult 还能这么用,学到了 :+1:

1 个赞

consult-dir 集成 zoxide 好像也不错

(use-package consult-dir
  :defer t
  :bind ((:map vertico-map
          ("C-x C-d" . consult-dir)))
  :config
  (setq consult-dir-default-command #'consult-dir-dired)

  (defun consult-dir--zoxide-dirs ()
    "Return list of zoxide dirs."
    (split-string (shell-command-to-string "zoxide query -l") "\n" t))

  (defvar consult-dir--source-zoxide
    `(:name "zoxide"
      :narrow ?z
      :category file
      :face consult-file
      :history file-name-history
      :enabled ,(lambda () (executable-find "zoxide"))
      :items ,#'consult-dir--zoxide-dirs)
    "zoxide directory source for `consult-dir'.")

  (add-to-list 'consult-dir-sources 'consult-dir--source-zoxide t))
4 个赞

尽量复用已有的工具, 毕竟模糊搜索, 匹配 consult 套件已经做的足够好了

都是同样的思路 :grinning_face:

我的补全框架还停留在 ivy 时代 :joy:

counsel应该也可以实现