3

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!

Alexander Kondratskiy
  • 4,156
  • 2
  • 30
  • 51
  • 1
    You have also this: https://github.com/bling/evil-visualstar (to install, or peek at the implementation) – VanLaser Sep 16 '16 at 15:37
  • 1
    Ah! That's much easier than cobbling my own stuff together. Thanks! You can answer with that, and I'll accept it. – Alexander Kondratskiy Sep 16 '16 at 16:00
  • 1
    You're welcome! and done. BTW, for emacs questions, there's also http://emacs.stackexchange.com/questions?sort=newest (dedicated) – VanLaser Sep 16 '16 at 16:06

1 Answers1

4

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

VanLaser
  • 1,144
  • 6
  • 8