Search code examples
vimemacsevil-mode

Emacs + Evil: non incremental search and persistent highlighting?


I'm trying Emacs+Evil after almost two decades as a Vim user. I'm moving most of my Vim configuration to Evil but one thing that I'm having a lot of problems is to set the search and highlighting like the one I use with Vim. What I'm trying to get is to have non-incremental search and the highlights to remain until I clear them manually or make another search.

I've set these settings on my config file:

;; keep the search highlights
(setq lazy-highlight-cleanup nil)
(setq lazy-highlight-max-at-a-time nil)
(setq lazy-highlight-initial-delay 0)

Using the / key to search with Evil does the incremental thing and also the highlights are removed as soon as I press any other movement key (like j key but with C-s (emacs internal i-search) the highlights remain. With C-s RET (non incremental search) the highlights doesn't remain.


Solution

  • Ok, found a working solution for the highlighting:

    (defun highlight-remove-all ()
      (interactive)
      (hi-lock-mode -1)
      (hi-lock-mode 1))
    
    (defun search-highlight-persist ()
      (highlight-regexp (car-safe (if isearch-regexp
                                      regexp-search-ring
                                    search-ring)) (facep 'hi-yellow)))
    
    (defadvice isearch-exit (after isearch-hl-persist activate)
      (highlight-remove-all)
      (search-highlight-persist))
    
    (defadvice evil-search-incrementally (after evil-search-hl-persist activate)
      (highlight-remove-all)
      (search-highlight-persist))
    

    This will highlight all searches done with isearch or Evil search. The highlight will remain until you make another one or call highlight-remove-all. I've mapped it to leader SPC with:

    (evil-leader/set-key "SPC" 'highlight-remove-all)
    

    PS: I made a package, it's already on melpa with the name "evil-search-highlight-persist" and: https://github.com/juanjux/evil-search-highlight-persist