Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

1 Answer

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

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>

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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