2

I have used curl to grab a website's code and put it in a text file. I'd like to delete all text that isn't a link! Lucky for me, all links are inside quotes, so I just need to delete all lines without https:// in them, which I have done using :g!/https:\/\//d. Now I need to delete everything to the left and right of the quotes.

Here is my dilemma:

LINE 1: RetailShop.URLs.getProductComponents = "https://example.com?emptyparam="; // random code
LINE 2: random code action="https://example.com" name="morerandomcode"

If I start by intending to delete everything to the left of the quotes, I would use something like this :%s/^.*"// but if you try it yourself, it wants to delete everything to the left of the second quote on line 1. That would delete my whole link. Let's say I use a space, :%s/^.* "// (after the * and before the "), then it would work on line 1, but not on line 2. Then when I do line 2 with :%s/^.*="// it will delete my link in line 1 because of how the link ends.

I don't want to complicate this too much. Explicitly, I need a command that deletes the FIRST quote in every line regardless of what characters come before it.

4
  • 5
    Vi and Vim would be a better place to ask.
    – Barmar
    Commented Aug 29, 2022 at 15:49
  • 1
    I didn't even know that existed, thank you.
    – ex7lted
    Commented Aug 29, 2022 at 15:51
  • More like an alternative place to ask now that SO clarified some of its rules.
    – romainl
    Commented Aug 29, 2022 at 16:03
  • Is the HTML you're running this on guaranteed to only have one link on each line? This obviously isn't true for webpages in general, but perhaps it is for you. I ask because you'll need to alter your technique if you want to handle multiple links per line.
    – Rich
    Commented Aug 30, 2022 at 15:28

1 Answer 1

2

I am not sure what exactly you want but, let's imagine this:

Before:

LINE 1: RetailShop.URLs.getProductComponents = "https://example.com?emptyparam="; // random code
LINE 2: random code action="https://example.com" name="morerandomcode"

After:

https://example.com?emptyparam=
https://example.com

The command is :%norm! f"di"Vp, which is

  1. % range for the whole buffer, following command would be run over all lines, one by one.
  2. norm! execute normal mode commands without mapped things, in it's original form.
  3. f" goto first "
  4. di" delete everything inside "..." and put it into unnamed register
  5. Vp select whole line and paste over it from unnamed register

PS, in modern vim you can omit f":

:%norm! di"Vp

PPS, if you really want to use regex, you can try following:

:%s#^.*\(https://\S\+\)".*#\1

Which will have the same effect (on a given example). It matches the whole line and submatches an https:// url, which is then used in replacement for the whole line.

2
  • I love your answer, I have nothing against using norm. One last thing though. If I have a line like so: post="null" href="example.com" it will delete the second set of quotes that I'd like to keep, how can I use norm to possibly keep only quotes that have https inside of them?
    – ex7lted
    Commented Aug 29, 2022 at 17:31
  • Then probably regex solution would be simpler, @ex7lted, anyway, here it is with norm: %norm! /https^Mdi"Vp, where ^M should be placed as CTRL-V CTRL-M
    – Maxim Kim
    Commented Aug 29, 2022 at 17:37

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.