You're mixing up a few concepts...
You can use
<C-R> from Insert or Command-line mode (when typing an Ex command after entering it with a
:) to insert the literal contents of a register. (See
So you can use that in a direct mapping (without the indirection of
:execute), like so:
nnoremap <Leader>c :%s/<C-R>//\=Count(submatch(0))/gn<CR>
When this mapping gets executed (with
<Leader>c, which defaults to
\c if you haven't set
<C-R>/ sequence will expand to the contents of the search register and will be inserted as part of the Ex command.
If, instead, you want to use
:execute (because you're not in the context of a mapping, for instance you're inside a function), then what you need is to assemble a string with the full command you want to execute.
In that case, you want to use
@/, which returns the contents of the search register as a string, that you can use as part of an expression. (See
:help expression-syntax, which has an entry for
@r that evaluates to "contents of register
Another issue with your attempt is that you had
Count() as a function call, but you actually want it to be part of the string, since it's only supposed to be evaluated by the
\= in the replacement of the
Putting it all together:
Finally, as pointed out in this comment from Christian Brabandt, in this particular case you don't need to expand the search register at all, since if you use a
:substitute command without a pattern (empty pattern), the last searched pattern will be used instead. So you don't actually need
execute at all, all you need is:
In a function, this command will work directly (it's a valid Ex command.) In a mapping, all you need is to prepend
: and add a
<CR> at the end. Pretty straightforward!