Neka
Neka

Reputation: 1664

how to replace string with result of function in Vim?

I want to insert filename and line number into some places in the file. For example this line:

_debug('init');

I want to replace

:s/debug('/debug('(%current_filename_here%:%current_line_number_here%)\ /g

to get this

_debug('(filename.ext:88) init');

I try to use expand('%:t') to get filename and line(".") to get line number, but I don't know how to use it in replace expression.

How can I do this?

Upvotes: 3

Views: 659

Answers (1)

kev
kev

Reputation: 161974

You can use \=. For example:

:s@_debug('\zs@\=printf('(%s:%d) ', expand('%:t'), line('.'))@

When the {replacement} starts with "\=" it is evaluated as an expression,

Upvotes: 8

Related Questions