Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim - call function inside 'replace' expression

Tags:

vim

I understand you can call a function in a vim search/replace operation. For example:

%s/regex/\=localtime()/g

will replace anything matching 'regex' with the current epoch time. The problem is, I can't add anything else to the 'replace' expression. For example:

%s/regex/epoch: \=localtime()/g

does not treat 'localtime' as a function anymore. Rather it just prints 'epoch: =localtime()' as a string in the replacement text. The intention is for it to print 'epoch: 1353085984' instead.

Is there any way to call a function from within an arbitrary section of a replacement expression?

like image 397
pedorro Avatar asked Nov 16 '12 17:11

pedorro


1 Answers

Once you use a replace-expression, it must be the entire replacement. You can use String concatenation in your expression (and refer to captured elements via submatch(1) instead of \1), like this:

:%s/regex/\='epoch: ' . localtime()/g
like image 90
Ingo Karkat Avatar answered Oct 02 '22 13:10

Ingo Karkat