3

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?

1 Answer 1

8

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,

3
  • I can not use Vim functions directly in expression, through printf only, right?
    – Neka
    Jun 6, 2012 at 15:44
  • You can use vim functions. @ is just a separator. It is customary to write the function as :s///, but :s### works as well, as does :s@@@...
    – Amadan
    Jun 6, 2012 at 19:03
  • @Neka printf is a function in vim. You can use \='('.expand('%:t').':'.line('.').') ', as you can see, I use . to concatenate multiple expression.
    – kev
    Jun 6, 2012 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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