In vimscript I've defined a variable like this:
let b:myvar = 'abc'
Now how can I insert the contents of that var into a search & replace, eg:
:s/123/&myvar/
Kent's answer works well for the replacement part; for generic insertion when typing the substitute command interactively, you can insert any expression (not just variables, also functions etc.) via <C-R><C-R>=
(these must be typed as Ctrl + R, Ctrl + R, =, not literally):
:substitute/<C-R><C-R>=b:myvar<CR>/replacement/<CR>
Inside a script, you'd use :execute
:
:execute 'substitute/' . b:myvar . '/replacement/'
try this line:
:s/123/\=b:myvar/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With