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.