4

After seeing this post and one of its comments, I am trying to implement the following convenient search pattern in dired:

  1. Press C-s to start isearch
  2. Type the substring I am looking for
  3. Possibly jump between all matching results by typing C-s repeatedly
  4. Press the "Enter" key to enter the file (or folder) highlighted by isearch

I'm doing it with the following snippet:

(add-hook 'isearch-mode-end-hook 
  (lambda ()
    (when (and (eq major-mode 'dired-mode)
               (not isearch-mode-end-hook-quit)
               (eq last-input-event ?\r)
               )
      (dired-find-file))))

Unfortunately the (eq last-input-event ?\r) bit does not appear to work: pressing "Enter" just exits isearch (default behavior), and I have to press it again to enter the file. For some reason ?\r does not capture my "Enter" key. To verify this is not caused by some other part of my emacs configuration I deactivated most of it, but to no avail.

Additionally, I would be interested to hear whether there is some other approach to search files in dired which convenience can be comparable to the one described above.

1 Answer 1

4

Change \r to 'return:

(add-hook 'isearch-mode-end-hook 
  (lambda ()
    (when (and (eq major-mode 'dired-mode)
               (not isearch-mode-end-hook-quit)
               (eq last-input-event 'return)) ; <==========
      (dired-find-file))))

(But it's better to use a named, not anonymous, function as a hook function. Easier to remove it etc.)


C-h k Enter says that RET is translated from <return>.

Elisp manual node Function Keys says:

Most keyboards also have “function keys”—keys that have names or symbols that are not characters. Function keys are represented in Emacs Lisp as symbols; the symbol’s name is the function key’s label, in lower case. For example, pressing a key labeled <F1> generates an input event represented by the symbol f1.

So function key <return> generates the input event represented by symbol return.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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