tags:

views:

35

answers:

2

I wish to use the content of a register as search pattern in Vim.

I would like to do this from the command line so I cannot use the <c-r> syntax since that assumes an interactive session.

It is possible to use a register as a replace pattern, like this

:%s/foo/\=@a/g

However using this syntax as a search pattern does not work

:%s/\=@a/foo/g 

it outputs

E64: \= follows nothing
E476: Invalid command
+1  A: 

I don't think it's possible directly, but you can use :exe to achieve this:

:exe '%s/' . @a . '/foo/g'
Al
+1  A: 

In a pattern (not the replacement part), \= stands for “0 or 1 time” (it is a synonym for \? put should be preferred over \? since the latter means a literal ? when looking backwards.

See :help /\= and help pattern for more details.

Why not :

let @/=@a
%s//foo/g
Benoit
I accepted Al's answer since it also works for other commands like ``vimgrep`` (which I was also looking to control).
pkit
No problem. Do not apologize when selecting an answer, we know you can only select one.
Benoit