9

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?

3 Answers 3

12

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.

7

In Ingos answer, which helped me a lot, I don't see how to reference a subpattern of the match and use it as an argument to the shell command ("produced by a console command with the expression as its argument").

In my case I had log lines of the following form: [<unix timestamp>] <message>, i.e. [1406844000] foobar. I wanted to prepend them with the time in a human readable form.

The following vim command matches the timestamp (submatch 1) and the message (the rest, submatch 2) and prepends the time string from date command:

:% s/^\[\(\d\+\)\] \(.*\)$/\=substitute(system('date -d @'.submatch(1)),'\n', '', 'g').': ['.submatch(1).'] '.submatch(2)/

[1406844000] foobar gets Fr 1. Aug 00:00:00 CEST 2014: [1406844000] foobar.

HTH

1
  • Awesome. Convert basic math to a result, at the touch of a key: vnoremap <leader>M y:%s/\C\<<C-r>"\>/\=substitute(system('expr '.submatch(0)),'\n','','g')/g (where math doesn't involve multiplication or division, which would have to be escaped somehow) Sep 23, 2022 at 11:20
2

For base64 substitution in a ldiff file:

:%s/:: \(.*\)$/\=substitute(': ' . system('echo '.submatch(1).'|base64 -d'),'\n','','g')/

You must log in to answer this question.

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