Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
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.)


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .