pedorro
pedorro

Reputation: 3357

vim - call function inside 'replace' expression

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?

Upvotes: 14

Views: 1702

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172778

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

Upvotes: 16

Related Questions