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

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))

Try this:

(setq lazy-highlight-cleanup nil)

If you want to clear out the highlight manually, do M-x lazy-highlight-cleanup


Trey's answer seems to work. I thought I'd include one using advice just for the sake of completeness:

(defadvice isearch-exit (after ysph-hl-search activate compile)
  "after isearch, highlight the search term "
  (highlight-regexp (car (if isearch-regexp
                             regexp-search-ring
                           search-ring)) (find-face 'hi-pink)))

Tags:

Emacs