0

I'm dealing with a tiny, yet annoying (to me) feature of Evil.

My evil-search-module is set to evil-search. Why that is relevant explained in this post When I invoke evil-search (bound to * in normal state), it always tries to search for a symbol. e.g, if I have cursor at FooBar - it will search for occurrences of 'FooBar' in the current buffer.

And even if I select only 'Foo' part of it, it would ignore my selection and search for the whole thing. Now, this isn't a bug, I checked, Vim does the same thing.

But is it possible (what am I saying? of course it is possible). How can I make it respect the selection? If I have selected 'Foo' it should search only for occurrences of 'Foo'; if I select 'Bar' - it should only search for 'Bar', and so on...

1 Answer 1

0

I was misled to think that this is a default behavior in Vim. Turns out Neovim does it (because it makes sense), but not Vim.

The feature I wanted called "vim-visual-star-search". I talked to Tom Dalziel, maintainer of Evil, and after a short discussion, we both agreed that this shouldn't be the default feature of Evil (since it's not in Vim).

It took me a while to make it work:

(defun evil-ex-visual-star-search-a (fn unbounded direction count &optional symbol)
  "Visual-star search in evil.

I want * and # operators to respect marked region."
  (if (use-region-p)
      (let* ((beg (region-beginning))
             (end (1+ (region-end)))
             (delta (- end beg 1))
             (s (buffer-substring beg end))
             (evil-ex-search-pattern s)
             (evil-ex-search-offset 0))
        (deactivate-mark)
        (if (eq direction 'forward)
            (goto-char end)
          (goto-char beg))
        (when (evil-ex-search-full-pattern s count direction)
          (let ((p (progn (when (eq direction 'forward)
                            (backward-char))
                          (point))))
            (set-mark p)
            (goto-char (if (eq direction 'forward)
                           (- p delta)
                         (+ p delta))))))
    (funcall fn unbounded direction count symbol)))
    
(advice-add #'evil-ex-start-word-search :around #'evil-ex-visual-star-search-a)

And only after I wrote that snippet I realized that I might not be the only one who wanted this. And indeed, the package already exists. https://github.com/bling/evil-visualstar

I'll be using my snippet for now (it's small and does what I want), but if it doesn't work for you for any reason, try the package.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.