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,

Sign up to request clarification or add additional context in comments.

3 Comments

I can not use Vim functions directly in expression, through printf only, right?
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@@@...
@Neka printf is a function in vim. You can use \='('.expand('%:t').':'.line('.').') ', as you can see, I use . to concatenate multiple expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.