Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim using contents of a variable inside search and replace expression

Tags:

vim

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/
like image 285
Magnus Avatar asked Feb 18 '13 17:02

Magnus


2 Answers

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/'
like image 189
Ingo Karkat Avatar answered Nov 02 '22 05:11

Ingo Karkat


try this line:

:s/123/\=b:myvar/  
like image 38
Kent Avatar answered Nov 02 '22 03:11

Kent