8

For example, I want to highlight occurrences of the current yank register, i.e.,

:let @/=@"

It works, unless @" has 'funny' stuff in it like backslash.

I can do

:let @/=escape(@", '\\')

but I'm not sure if this is the right thing to do, and if it will escape all possible problematic characters. Maybe there are others.

Update: ok, I really need to escape more stuff. all the regent special characters at least it seems. so is there a regexp escaping function or variable listing all regexp special chars?

1 Answer 1

5

I don't think there is such a list. Here is the escape function I have used for quite a while as part of a macro to search for the currently-selected string:

escape(@", '\\/.*$^~[]')

Edit:

Original answer had extra arguments due to my copying too much from my ~/.vimrc. Here is the mapping in which I used that escape in case having that context may help. Highlighting some text and typing * searches for the next occurrence of that text. (Mapping originally written by Jürgen Krämer.)


vnoremap <silent> * :<C-U>
          \let old_reg=getreg('"')<bar>
          \let old_regmode=getregtype('"')<cr>
          \gvy/<C-R><C-R>=substitute(
          \escape(@", '\\/.*$^~[]'), '\n', '\\n', 'g')<cr><cr>
          \:call setreg('"', old_reg, old_regmode)<cr>

2
  • hmm. looks like you have unmatched ) Aug 8, 2011 at 20:30
  • Within single quotes, you can get away with just one backslash. escape(@", '\/.*$^~[]')
    – Jasha
    Apr 2, 2022 at 7:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .