Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to have replace function which result will be used as replacement, and it takes match as an input.

This is something available in some languages (Lua for instance).

For instance, imagine I have a list of sites in a text and I want to replace them with ping values.

share|improve this question
    
Have you been able to write a function and just don't know how to use it in find replace syntax? –  Jeff Bridgman Dec 13 '12 at 17:53
1  
What? Could you give us an example of the desired behaviour? –  romainl Dec 13 '12 at 17:54
    
Re-reading your question, are you looking for :help substitute() or… simply :s/foo/bar? –  romainl Dec 13 '12 at 19:13
add comment

1 Answer

up vote 4 down vote accepted

You can make your replacement pattern an expression by starting it with \=, so you can call functions that way and pass to that function sub-matches with the submatch() function, for example:

:%s/foo .*/\=Foo(submatch(0))/g

(Remember that sub-match 0 always means the whole matched pattern.)

See:

:help sub-replace-expression
:help submatch()
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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