\V
is ideal when you can use it. But on some occasions you might want to escape a string, and then include it inside a regular expression that does use magic characters.
In such situations (1), I have been using a function like this:
function! EscapeForVimRegexp(str)
return escape(a:str, '^$.*?/\[]~')
endfunction
I'm not sure if that has escaped everything it needs to, so please let me know if you find any example which fails!
Note that if your regular expression is going to be used by GNU grep, rather than as a Vim regular expression, then you may need to escape some different things:
function! EscapeForGNURegexp(str)
return escape(a:str, '^$.*?/\[]()' . '"' . "'")
endfunction
" Example usage
command! FndFunc exec 'grep "\<' . EscapeForGNURegexp(expand('<cword>')) . '(" . -r' | cope