How can I get emacs to keep my isearch strings highlighted?

Viewed 2687

How can I get emacs to highlight the phrase I'm searching for and then keep it highlighted until I search for another phrase? Can it do this transparently i.e. just by searching, not having to run another command afterwards (like isearch-highlight-regexp) ?

4 Answers

This variant combines lazy-highlight with regex highlighting once you're done with the search. This resembles the hlsearch behavior of vim in emacs.

(setq lazy-highlight t                 ; highlight occurrences
      lazy-highlight-cleanup nil       ; keep search term highlighted
      lazy-highlight-max-at-a-time nil ; all occurences in file
      isearch-allow-scroll t           ; continue the search even though we're scrolling
      )

;; when exiting isearch, register the search term as regexp-highlight
(defadvice isearch-done (after ysph-hl-search activate compile)
           "highlight the search term after isearch has quit"
           (unhighlight-regexp t)
           (highlight-regexp (car (if isearch-regexp
                                      regexp-search-ring
                                      search-ring)) 'lazy-highlight))
Related