For example, say I have the following string:

my_url = "http://somewebsite.com"

and I want to use a regex to replace the url with a version from a url shortener. I've written a command line script, "url_shortener", that takes a url as an input and returns the shortened version.

Is there any way to call something like:

:s/\(http[^"]\+\)/system(url_shortener \1)/g

This is for vim or neovim

  • See :help sub-replace-\=.
    – romainl
    Feb 14, 2023 at 6:03

1 Answer 1


I would do:

:s/\(http[^"]\+\)/\=system('url_shortener ' . submatch(1))/g

If you use \= the following expression is evaluated and used for the substitution.

  • 1
    seems close, but how do you pass the first match to system? It seems system would be unaware of the regex match. Feb 14, 2023 at 6:28
  • 1
    Also, system seems to require a string argument, e.g. system("echo hello") Feb 14, 2023 at 6:32
  • 2
    I have corrected the solution :-/ I wrote on my sofa when I wake up, sorry for that :-) Feb 14, 2023 at 6:48
  • 1
    wow. very nice. thank you!! Feb 14, 2023 at 7:30

Your Answer

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

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