Dave Lens
Dave Lens

Reputation: 38

How do I use Vim's escape() with <C-R> as its first argument?

My goal is to select several words in Vim's visual mode (well, neovim in my case), press leader+L and let fzf show search results for the selected string through :Rg. I came up with this:

vnoremap <expr> <leader>l 'y:<C-U>Rg '. shellescape(escape('<C-R>"', '()[]><')) .'<CR>'

Which does work, but when I select the text options(:modifier) and trigger a search, the escape() command doesn't escape the parentheses and Rg fails to return results.

In short, I'm expecting this command to fire:

:Rg 'options\(:modifier\)'

And I'm getting this instead:

:Rg 'options(:modifier)'

I'm guessing I can't use <C-R> in this context, but I can't seem to figure out why?


UPDATE: Thanks to a helpful reply from user D. Ben Knoble indicating I could drop and construct the mapping differently, I ended up with this, solving my problem:

vnoremap <leader>l "ky:exec 'Rg '. shellescape(escape(@k, '()[]{}?.'))<CR>

Upvotes: 1

Views: 215

Answers (1)

D. Ben Knoble
D. Ben Knoble

Reputation: 4703

You don’t need to—all registers are available as variable’s prefixed with @ (all are readable except @_, most are writable, I think).

So instead of <C-R>", use @"

Upvotes: 3

Related Questions