Use case value as a search pattern

I want to use the contents of the register as a search template in Vim.

I would like to do this from the command line, so I cannot use the <cr> syntax, as this implies an interactive session.

You can use case as a replacement pattern, for example

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

However, using this syntax as a search pattern does not work

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

displays

 E64: \= follows nothing E476: Invalid command 
+22
vim
Oct 13 '10 at 9:44
source share
3 answers

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

 :exe '%s/' . @a . '/foo/g' 
+11
Oct 13 '10 at 9:59
source share

In the template (and not in the replacement part), \= means "0 or 1 time" (this is a synonym for \? Put should be preferable to \? Since the latter means a literal ? When you look back.

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

Why not:

 let @/=@a %s//foo/g 
+11
Oct. 13 '10 at 10:44
source share

You can do this using the register /

This does not solve the general problem, but it seems to me that the / register is special in this regard: if you leave an empty space, it will use everything that is in the / register; namely, whatever you are looking for the last time.

So, if you want to replace foo with the contents of @a , you can do this:

  • Search foo
  • %s//\=@a/g (or select the text and start typing :s to replace only in this range)
+10
Aug 03 2018-12-12T00:
source share



All Articles