0

I'm looking for a way to copy link (http URL) that is under cursor. I'm aware that I can copy the whole line with ctrl-c when nothing is selected, but in my case links are in the middle of text.

Example: wikipedia: https://en.wikipedia.org/wiki/Main_Page - the great encyclopedia

VScode recognize it's a link because it allows to open it, but doesn't offer to copy it to clipboard

I've searched for an extension to achieve this but I didn't found unfortunately.

Bruno Duyé
  • 1,014
  • 11
  • 13

1 Answers1

1

You can use the extension Select By

Use the command: selectby.regex

Call by command palette

If you call the command from the Command Palette define the regex in the setting: selectby.regexes.

In setting.json add:

    "selectby.regexes": {
      "selectHTTPs": {
        "surround": "https?://[\\w;,/?:@&=+$\\-.!~*'()#]+",
        "copyToClipboard": true
      }
    }

Call with a shortkey

If you call the command with a key binding you can add these arguments to the key binding:

In keybindings.json add:

    {
        "key": "ctrl+shift+c",
        "when": "editorTextFocus",
        "command": "selectby.regex",
        "args": {
            "surround": "https?://[\\w;,/?:@&=+$\\-.!~*'()#]+",
            "copyToClipboard": true
        }
      }

This uses a very basic HTTP(S) URL regex.

The selection is also copied to the clipboard, no need to use Ctrl+C

Maybe somebody on the internet has written a more elaborate URL regex.

Or you can browse the VSC source to locate the DocumentLinkProvider that scans for URLs.

Edit

I looked for allowed characters and adjusted the regex. I did not know () are allowed.

Characters allowed in a URL

https://www.freecodecamp.org/news/url-encoded-characters-reference

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • Thanks ! This works Note: as settings file is a Json file, backslash must be protected: "surround": "https?://[^\\s]+" – Bruno Duyé Apr 08 '23 at 16:15
  • @BrunoDuyé thanks for the edit and double backslash, I looked for allowed characters and modified the regex, I don't know if `:` is allowed outside the protocol – rioV8 Apr 08 '23 at 19:10