1

Want to exactly matching current word with below function.

function! My_Exact_Search ()
   let CurrentWord = expand("<cword>")
   exec "/\<".CurrentWord."\>"
endfunction

But get error: E486: Pattern not found: <word>.
How to fix it?

1

The * command does this natively.

The problem with your function is you need to escape your / or use literal-string (I recommend).

let @/ = '/\<'.CurrentWord.'\>'

You also should be using :normal to create the search.

execute "normal! /\\<".expand('<cword>')."\\>\<cr>"

For more help see:

:h *
:h literal-string
:h quote/
  • Yes, * is working. Tried exec '/\<'.CurrentWord.'\>', but nothing happened. – Fisher Sep 4 '18 at 15:27
  • I just tried :execute '/\<'.expand('<cword>').'\> and it works for me – Peter Rincker Sep 4 '18 at 15:54
  • Yes, directly run the command is working, but if run from function then nothing is happening. – Fisher Sep 4 '18 at 16:05

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.