Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: Combine iseach-forward and recenter-top-bottom

Tags:

search

emacs

Thank you very much in advance for helping.

In Emacs, I like to use iseach-forward (C-s) but I'd like it ever more if the highlighted fount words would be centered in the middle of the screen instead of at the very bottom.

I find myself doing this continuously:

C-s foo C-s C-s C-s... oh! that's the 'foo' I was looking for! C-l

Is there a way to center the search results in the middle of the screen?

Cheers

like image 282
RafaelGP Avatar asked Jun 15 '12 14:06

RafaelGP


2 Answers

For other folks like me who came here looking to configure their spacemacs, @RafaelGP's answer worked for me, with a slight modification: all of the evil functions have -ex- in them, like this:

 (defadvice
      evil-ex-search-forward
      (after evil-search-forward-recenter activate)
    (recenter))
  (ad-activate 'evil-ex-search-forward)

  (defadvice
      evil-ex-search-next
      (after evil-search-next-recenter activate)
    (recenter))
  (ad-activate 'evil-ex-search-next)

  (defadvice
      evil-ex-search-previous
      (after evil-search-previous-recenter activate)
    (recenter))
  (ad-activate 'evil-ex-search-previous)
like image 72
Manas George Avatar answered Oct 23 '22 15:10

Manas George


Best approach would probably be to add the following hook:

(add-hook 'isearch-mode-end-hook 'recenter-top-bottom)

This will execute the recenter-top-bottom command at the completion of every successful incremental search.

Edit: I've investigated a bit, and the functions that are executed on repeated searches for the same string (i.e., with successive input of C-s or C-r during an active search) appear to be isearch-repeat-forward and/or isearch-repeat-backward. Hence, if you wish to recenter on every repeat as well, you need to advise said functions in addition to defining the above hook, like so:

(defadvice
  isearch-repeat-forward
  (after isearch-repeat-forward-recenter activate)
  (recenter-top-bottom))

(defadvice
  isearch-repeat-backward
  (after isearch-repeat-backward-recenter activate)
  (recenter-top-bottom))

(ad-activate 'isearch-repeat-forward)
(ad-activate 'isearch-repeat-backward)

Personally, I find the resulting behavior to be extremely annoying and disorienting, but de gustibus non est disputandum. Perhaps reserving recenter-top-bottom for use in the initial isearch-mode-end-hook and using recenter alone in the advice to the repeat functions would be less obnoxious.

Advising isearch-forward by itself is equivalent to adding the hook I originally suggested above and seemingly has no effect on repeat searches. Adding the hook is simpler and I think more idiomatic, so it should probably be preferred over advising the function.

like image 41
Greg E. Avatar answered Oct 23 '22 16:10

Greg E.