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
) ?
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)))
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With