Search code examples
vimemacselispevil-mode

Search visual selction in emacs evil


I want to emulate a behavior I've had in vim, but in emacs evil-mode. I want * to search for the current visually selected text. The code below sort of works, but pressing n or N does not retain the search string

(define-key evil-visual-state-map (kbd "*")
  (lambda () (interactive)
    (let ((search-string (buffer-substring
                          (evil-range-beginning (evil-visual-range))
                          (evil-range-end (evil-visual-range)))))
      (evil-normal-state)
      (evil-search search-string t))))

I'm new to emacs/elisp. Any ideas on what the "right way" to do this is? In the evil-search.el source I see a function evil-ex-search-update-pattern that may be useful, but I'm not sure how to put it together.

Thanks!


Solution

  • Bailey Ling (a former Vim user) has made a plugin that accomplishes the same thing you're after: https://github.com/bling/evil-visualstar.

    You can either install it, or peek through the source code (which may help solve your issue).