24

I'm replacing all the inline gist snippets w/ a div to load them in a non blocking way. To modify all the legacy articles that have the following

<script src='https://gist.github.com/1234.js?file=gistfile1.sh'></script>

I need to replace it with the following instead

<div data-gist=1234><a href='http://gist.github.com/1234'>gistfile1.m</a></div>

So far what I'm trying (vim newb here) -doesn't seem to work

:%s/<script src='https:\/\/gist.github.com\/(d+).js?file=gistfile1.sh'><\/script>/<div data-gist={1}><a href='http://gist.github.com/{1}'>gistfile1.m</a></div>//g
0

1 Answer 1

64

I got it to work with this:

:%s!<script src='https://gist.github.com/\(\d\+\).js?file=gistfile1.sh'></script>!<div data-gist=\1><a href='http://gist.github.com/\1'>gistfile1.m</a></div>!g

Couple things to note:

  1. I used ! instead of / as seperator to avoid having to escape path slashes
  2. You need to escape (, d, +, and ) in your attempt.
  3. You had extra / at the very end.
  4. To insert the match group, use \1 instead of {1}
Sign up to request clarification or add additional context in comments.

6 Comments

nice! the only thing to note is you need to start that with :%s
one quick edit -how can I ignore the file= (name of file varies) and I don't need this during the replace so it could be lost (sorry to scope creep) **turns out I had various file names for each gist and they don't actually mean much for this implementation
If you don't care for the file name, you can just do file=[^']\+'. That will match everything after file= until the next '. If you want to use the name in the second part, then wrap another capture group around it ().
You are the man! thanks for the speedy and documented answer!
someone give bounty... :)
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.