How to recenter in +vertico/embark-preview of selected candidate in buffer?

What happened?

I did SPC-s-p to search inside a project using Projectile. A list of candidates are shown in the minibuffer. I selected one candidate and hit C-SPC to do preview, which I see is mapped to +vertico/embark-preview, and the preview of the file is shown in the main buffer with the line flashed and highlighted. I’d like to center it like how helm does it, but I couldn’t get it working using any of these settings in my config.el file.

(add-hook! '+vertico/embark-preview 'recenter)
(add-hook! '+vertico/embark-preview 'recenter-top-bottom)
(add-hook! 'consult-preview-at-point-mode 'recenter)
(add-hook! 'consult-preview-at-point-mode 'recenter-top-bottom)

What did you expect to happen?

The selected candidate is shown with matched line centered in the main buffer.

Steps to reproduce

  1. Add a project to Projectile.
  2. Open project.
  3. Do a search on a project via SPC-s-p.
  4. Select a candidate in minibuffer and hit C-SPC to have its file opened in main buffer with matched line centered.

System information


Loading data dump...
1 Like

Hm, I cannot repdocue this. For me, the line is centered (if possible) when pressing C-SPC. I do not have any extra configuration with respect to vertico or consult.

What happens exactly in your case? Where is the line? At the top/bottom?

I cannot reproduce this.

I can!

The line is never centered - the buffer only scrolls enough for it to appear on the screen, top or bottom.

generated  Sep 22, 2022 09:28:57
system     MacOS 12.5.1 Darwin 21.6.0 arm64
emacs      28.2 ~/.doom.emacs.d/ -> ~/.doom.emacs.d/
doom       3.0.0-pre PROFILE=_@0 HEAD -> master, origin/master, origin/HEAD,
	  _upgrade/master, _upgrade_master e9ef904ee 2022-09-21 01:05:34 +0200
	  ~/.config/doom/ -> ~/config/emacs/.config/doom/
shell      /bin/zsh
features   ACL GLIB GMP GNUTLS JPEG JSON LIBXML2 MODULES NATIVE_COMP NOTIFY
	  KQUEUE NS PDUMPER PNG RSVG THREADS TIFF TOOLKIT_SCROLL_BARS ZLIB
traits     batch envvar-file custom-file
modules    :completion company vertico :ui doom doom-dashboard doom-quit hl-todo
	  hydra minimap modeline nav-flash ophints (popup +defaults) (treemacs
	  +lsp) vc-gutter vi-tilde-fringe window-select workspaces zen :editor
	  (evil +everywhere) file-templates fold (format +onsave) lispy
	  multiple-cursors snippets word-wrap :emacs (dired +dirvish) electric
	  undo vc :term vterm :checkers syntax spell grammar :tools (debugger
	  +lsp) direnv docker editorconfig (eval +overlay) lookup lsp (magit
	  +forge) make taskrunner :os macos :lang beancount (clojure +lsp)
	  emacs-lisp json (javascript +lsp) kotlin (lua +fennel) markdown nix
	  (org +roam +dragndrop +pandoc) (rust +lsp) sh web (yaml +lsp) :config
	  (default +bindings +smartparens)
packages   (magit :pin f331092df4d4dfc0a2a7424d929a9c845088d57f) (magit-delta)
	  (framemove) (bury-successful-compilation) (language-id)
	  (graphql-mode) (evil-lisp-state) (conventional-commit :recipe (:repo
	  akirak/conventional-commit.el :host github)) (ox-gfm)
	  (org-pandoc-import :recipe (:host github :repo
	  tecosaur/org-pandoc-import :files (*.el filters preprocessors)))
	  (eslint-fix) (org-modern) (evil-matchit) (flycheck-jest) (nix-update
	  :recipe (:repo aisamu/nix-update-el :host github)) (separedit :recipe
	  (:repo twlz0ne/separedit.el :host github)) (org-appear :recipe (:host
	  github :repo awth13/org-appear)) (smithy-mode)
unpin      nix-update dirvish
elpa       vterm

This is interesting. The only thing I notice right away is that you are using Mac OS, and I don’t.

generated  Sep 22, 2022 14:59:54
system     NixOS 22.11.20220922.ae8cfc3 (Raccoon) Linux 5.15.68 x86_64 x
emacs      28.2 master emacs-28. ~/.emacs.d/
doom       3.0.0-pre PROFILE=_@0 HEAD -> master e9ef904ee 2022-09-21 01:05:34 +0200
           ~/.doom.d/
shell      /run/current-system/sw/bin/bash
features   CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON LIBOTF
           LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP NOTIFY INOTIFY
           PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS X11 XAW3D
           XDBE XIM XPM LUCID ZLIB
traits     gui server-running envvar-file
modules    :completion (company +childframe) (vertico +icons) :ui doom doom-dashboard
           doom-quit hl-todo modeline ophints (popup +defaults) vc-gutter
           vi-tilde-fringe :editor (evil +everywhere) (format +onsave)
           multiple-cursors snippets :emacs dired electric undo vc :term vterm
           :checkers syntax (spell +aspell +everywhere +flyspell) grammar :tools
           biblio direnv (eval +overlay) (lookup +dictionary) lsp magit make pass
           tree-sitter :lang (cc +lsp +tree-sitter) data emacs-lisp (haskell +lsp)
           (json +lsp) (latex +lsp) markdown (nix +lsp) (org +dragndrop +hugo +roam2)
           (python +lsp +tree-sitter) (rust +lsp) (sh +lsp +tree-sitter) (web +lsp)
           (yaml +lsp) :email mu4e :config (default +bindings +smartparens)
packages   (biblio) (emacsql-sqlite :built-in 'prefer) (inheritenv) (khardel)
           (ob-mermaid) (openwith) (smali-mode :recipe (:host github :repo
           strazzere/Emacs-Smali :files (smali-mode.el) :nonrecursive t)) (systemd)
           (unfill)
unpin      rustic
elpa       vterm emacsql-sqlite emacsql

I double checked my configuration file and the only vertico customization is a keymap change :face_with_diagonal_mouth:.

Just to be super sure, are you visiting files longer than the buffer height? (otherwise they appear to be “centered” by virtue of not being able to be scrolled)

Yes, the visited files are longer than buffer height. Matched line is shown enough in the buffer where it is visible so it could be top, almost middle, or bottom and if the second matched line from the same file is visible in the same view, it’s highlighted so it means it could be anywhere without being recentered.

@EmacsNoob the UI doesn’t help in making that explicit, but my last message was a reply to @dschrempf.

I observe the exact behavior you describe on my local installation.

@aisamu I found out that if I click on the buffer after C-SPC and then do C-L to do recenter-top-bottom, I could get it centered, but it’s really cumbersome to do that every time when you’ve multiple files to go through and will lose cursor focus from switching buffers.

Yes, I used buffers with more lines than the screen height.

In case of SPC s p (+default/search-project), +vertico/embark-preview delegates preview to consult, which runs consult-after-jump-hook after preview jump and has recenter command hooked up by default.

So, check consult-after-jump-hook value.

1 Like

Great pointer, thanks!

Apparently, nav-flash is the “culprit” doomemacs/config.el at 594d70292dc134d483fbf7a427001250de07b4d2 · doomemacs/doomemacs · GitHub

Removing that consult hook from the list did the trick (and so did re-adding recenter to consult’s hook)

Thanks @aisamu @organic-bookworm! Your solutions work! I tried with nav-flash and I also tried with nav-flash and adding in the hook to consult-after-jump-hook. Doom Emacs community is great, and I really appreciate the fast solutions! Many thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.