0

Is it possible to do something similar to these vim commands with emacs-evil? They basically do the same thing as * and visualstar of vim (and evil) but they do not move the cursor to the next match.

" 1. get current word on the cursor's position `expand('<cword>')`
" 2. wrap it with `'\<'` and `'\>'` to mark it as a word
" 3. put it in the search register `/`
" 4. highlight the matches
noremap <silent>* :let @/ = '\<'.expand('<cword>').'\>'\|set hlsearch<CR>

" 0. call the function that does step 1~3
" 1. copy the selected region into `"` register: `execute "normal! gvy"`
" 2. escape some characters in the copied text with `\`
" 3. put it in the search register `/`
" 4. highlight the matches
vnoremap <silent>* :<C-u>call Searchgvy()\|set hlsearch<CR>
function! Searchgvy()
    " raw text on `"`, escaped text on `/`
    execute "normal! gvy"
    let l:pattern = escape(@", "\\/.*'$^~[]")
    let @/ = l:pattern
endfunction
  • I don't see why not, Emacs Lisp is a fully-featured programming language. You'll probably get more feedback if you explain what exactly that Vimscript does since this is an Emacs, not Vim Stackexchange. – wasamasa Aug 29 at 18:30
  • @wasamasa thanks for the comment. I just updated the description. – Blank_ Sep 1 at 8:07
0

If I understood correctly, you want to press * to highlight the matches while keeping the cursor in the same place, and afterwards pressing n or N to move to next or previous match, right? You can do that with the following code:

(defun my/star-keep-position ()
  (interactive)
  (case evil-search-module
    (evil-search (progn
                  (evil-ex-search-word-forward)
                  (evil-ex-search-previous)))
    (isearch (progn
               (evil-search-word-forward)
               (evil-search-previous)))))

(defun my/visualstar-keep-position ()
  (interactive)
  (when (region-active-p)
    (evil-visualstar/begin-search (region-beginning) (region-end) t)
    (case evil-search-module
      (evil-search (evil-ex-search-previous))
      (isearch (evil-search-previous)))))

(evil-define-key 'normal evil-motion-state-map (kbd "*") 'my/star-keep-position)
(evil-define-key 'visual evil-visualstar-mode-map (kbd "*") 'my/visualstar-keep-position)
  • Thanks for the answer but it didn't work. I also tried something similar to this with (evil-goto-mark ?`) to emulate noremap * *`` but it seems to be broken too. – Blank_ Sep 1 at 8:15
  • That's strange, it seems to work as intended for me. Did you try in normal mode? This code will only work when you press * in a visual mode. If that's the issue, let me know and I'll make it work with normal mode too. – Jesse Sep 2 at 14:09
  • Do you use (evil-select-search-module 'evil-search-module 'evil-search) ? Your solution works if I remove that line from my config but I need it for evil-next-match (aka gn of vim). I guess I should try reading the evil code to figure out what's going on. – Blank_ Sep 3 at 4:07
  • Ah, I see. I edited the answer to make it work with both modules. Let me know if that works for you – Jesse Sep 3 at 12:21
  • It works perfectly. Thanks a lot! Just submitted an edit request with the keep-position version of normal mode command. – Blank_ Sep 4 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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