Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace string with result of function in Vim?

Tags:

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?

like image 761
Neka Avatar asked Aug 20 '26 19:08

Neka


1 Answers

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,

like image 91
kev Avatar answered Aug 22 '26 10:08

kev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!