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.

let's say I have a file with the following content:

This is a _file_ which has some _special_ keywords, that I would _like_ to replace.

I can do a search and replace of the speical keywords with "%s/_*_/something", but I would like "something" to be an output of an external command. Is this possible in vim, or do I need to awk it?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

Yes, you can. See :help sub-replace-special; the \= allows you to replace with a Vim expression. And system() can execute an external command and return its output. See my trivial example with hostname. Note that I had to use substitute() to get rid of the trailing newline character(s) in the output.

:%s/_[^_]*_/\=substitute(system('hostname'),'\n','','g')/g

PS: I also corrected your regexp pattern to match your example.

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.