2

I don't find any hook related to dired reloading. I tried after-revert-hook but it doesn't work.

1

I tried after-revert-hook but it doesn't work.

The hooks before-revert-hook and after-revert-hook are guaranteed to be run only by the default revert-buffer-function. See (elisp) Reverting.

I don't find any hook related to dired reloading.

Dired sets the function dired-revert as its revert-buffer-function. Like other Dired functions which read in directories, dired-revert runs the hook dired-after-readin-hook:

dired-after-readin-hook is a variable defined in ‘dired.el’.
Its value is (dired-omit-expunge)
Original value was nil

  This variable may be risky if used as a file-local variable.

Documentation:
Hook run after each time a file or directory is read by Dired.
After each listing of a file or directory, this hook is run
with the buffer narrowed to the listing.

You can customize this variable.

Nevertheless, this hook is not suitable for running recenter as it is run in a narrowed buffer.

How can I run (recenter) after dired reloads current buffer?

One solution is to advise dired-revert:

(define-advice dired-revert (:after (&rest _) my-recenter)
  "Call `recenter' after `dired-revert'."
  (recenter))

or similarly before Emacs 25:

(defun my-recenter (&rest _)
  "Wrap `recenter' while ignoring arguments."
  (recenter))

(advice-add #'dired-revert :after #'my-recenter)
| improve this answer | |
  • Hmm, after advicing the dired-revert call, I get apply: ‘recenter’ing a window that does not display current-buffer. when first opening dired on some directory. The second time works fine though. – Amos Oct 28 '17 at 17:02
  • @Amos Can you reproduce it if you start Emacs with the -Q flag? Because I can't reproduce it on Emacs 24, 25 or 27 or using any one of dired, dired-other-window or dired-other-frame. – Basil Oct 28 '17 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.