hi,好久不见,最近尝试从ivy转到vertico,遇到了部分问题,有无大佬可以解答
- 无法设置
initial
,因为 consult
大部分命令都是把项目目录和当前目录混在一起,所以我添加了当前目录的查询
(defun maple/consult-grep (&optional dir initial)
(interactive "P")
(consult-ripgrep (or dir default-directory) initial))
(defun maple/consult-project-grep (&optional dir initial)
(interactive "P")
(consult-ripgrep dir initial))
但是这样后如果我想要增加默认输入,我必须添加多个命令
(consult-customize
consult-line
consult-find
consult-ripgrep
maple/consult-grep
maple/consult-project-grep
:initial (thing-at-point 'word))
而且一旦使用其它命令调用 consult-ripgrep
,即使我只是增加了一个alias
(defalias 'maple/projectile-grep 'maple/consult-project-grep)
默认输入也无法生效,有无办法只用添加 consult-ripgrep
就行,不用修改 consult-customize
project-find-file
有无替代的函数,原来使用ivy时用的是 counsel-git
,但是 consult
没有相关函数,project-find-file
或者 projectile-find-file
卡顿相当明显,类似 consult-find
又只能在输入参数后才能查询
最后,虽然还在使用ivy,也分享几个vertico的好用配置
;;insert or done when multi tab
(defun maple/vertico-done ()
(interactive)
(when (> vertico--total 0)
(let* ((vertico--index (max 0 vertico--index))
(cand (vertico--candidate)))
(if (equal (minibuffer-contents-no-properties) cand)
(vertico-exit)
(insert (prog1 cand (delete-minibuffer-contents)))))))
;;窗口左右分屏时不用移动视野
(defun maple/vertico-format(x)
(concat (make-string (window-left-column (minibuffer-selected-window)) ?\s) x))
(advice-add #'vertico--format-count :filter-return #'maple/vertico-format)
(advice-add #'vertico--format-candidate :filter-return #'maple/vertico-format)
2 个赞
帅,这个可以有,我改了一下,增加了对 nerd-icons-completion
的支持
(defun maple/consult-git ()
"Find file in the current Git repository."
(interactive)
(let* ((default-directory (project-root (project-current)))
(cmd "git ls-files -z --full-name --")
(cands (split-string (shell-command-to-string cmd) "\0" t))
(file (completing-read "Find file: " (project--file-completion-table cands) nil t)))
(find-file file)))
还是找到了解决方案,详情可看 maple-emacs/lisp/init-completion.el at master · honmaple/maple-emacs · GitHub
- 有两个方案,一个是自定义命令修改
this-command
(defun maple/consult-grep (&optional dir initial)
(interactive "P")
(let ((this-command 'consult-ripgrep))
(consult-ripgrep (or dir default-directory) initial)))
另外一种方案比较hack,直接修改 completing-read
函数,这种方案可能会导致所有minibuffer相关的命令都添加默认输入,虽然可以立即删除
(defun maple/consult-read (oldfunc &rest args)
(minibuffer-with-setup-hook
(lambda() (add-hook 'after-change-functions 'maple/consult-initial nil t))
(let ((initial (nth 4 args)))
(while (< (- (length args) 5) 0)
(setq args (append args (list nil))))
(when (or (not initial) (string= initial ""))
(setf (nth 4 args) (propertize (maple-region-string) 'face 'shadow)))
(apply oldfunc args))))
(defun maple/consult-initial(&optional beg end len string)
(remove-hook 'after-change-functions 'maple/consult-initial t)
(when (member this-command '(self-insert-command))
(let ((str (buffer-substring-no-properties beg end)))
(when (eq 'file (vertico--metadata-get 'category))
(setq str (minibuffer-contents-no-properties)))
(delete-minibuffer-contents)
(insert str))))
(advice-add 'completing-read :around 'maple/consult-read)
- 除上面的
git ls-files
外,我还是喜欢使用 rg --files
,这样就能不限制是否是git管理的目录
(defun maple/file-completion-table (cands)
(lambda (string pred action)
(cond
((eq action 'metadata)
'(metadata . ((category . file))))
(t
(complete-with-action action cands string pred)))))
(defun maple/find-file ()
(interactive)
(let* ((cmd "rg --files --color=never")
(cands (split-string (shell-command-to-string cmd) "\n" t))
(file (completing-read "Find file: " (maple/file-completion-table cands) nil t)))
(find-file file)))
ok,问题解决后多试几天,看看和ivy有啥差距
1 个赞