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))
Another option is to just use highlight-phrase or highlight-regexp, and not depend on the search system doing the job at all.
精彩评论