7

Is it possible to pass a variable to a function in vimscript, and then use that value in a search and replace regex?

For example:

function! AddTW5Links(tiddlerSetName) 

    %s/\(*\+\)\(\[\[\(.*\)\)\]\]/\1[[\3|\3 - $tiddlerSetName]]/g

endfunction
0

1 Answer 1

8

You have the choice between using :s after :exe, or using :s\=.

Meaning (untested)

:exe 's/\(*\+\)\(\[\[\(.*\)\)\]\]/\1[[\3|\3 - '.a:tiddlerSetName.']]/g'

(beware of double-quote, you'll have to double all your backslashes with them)

Or

:s/\(*\+\)\(\[\[\(.*\)\)\]\]/=submatch(1).'[['.submatch(3).'|'.submatch(3).' - '.a:tiddlerSetName.']]'/g

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.