Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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(@", '\\/.*$^~[]')


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>
          \escape(@", '\\/.*$^~[]'), '\n', '\\n', 'g')<cr><cr>
          \:call setreg('"', old_reg, old_regmode)<cr>

share|improve this answer
hmm. looks like you have unmatched ) – Vitaly Kushner Aug 8 '11 at 20:30
Yes. Thanks. Copy and paste error. I'll fix. – garyjohn Aug 8 '11 at 21:42

You must log in to answer this question.

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