24

from other text editors I'm used to adding Markdown links by

  1. selecting the word I want to be linked,
  2. pressing cmd-K on my Mac's / iPad Pro's keyboard, which puts square brackets around the marked word, appends a pair of normal parenthesis () and places the cursor right in beetween those two parenthesis so that I can
  3. just paste the URL I have in my clipboard into the right place by pressing cmd-V.

So, select -> cmd-K -> cmd-V is a nice and short sequence for adding links in a Markdown document and cmd-K has become some kind of pseudo standard for adding links in several writing apps.

However, in VSCode that's not possible. But I'd love to make it possible. Any ideas? cmd-K is (hard-wired?) bound to listen for a next key press. But it doesn't have to be cmd-K. I can learn another keystroke. But I need to be able to put additional text (square brackets and parenthesis) into the text and move the cursor to the right position. How's that done?

Thanks so much!

3 Answers 3

33

This extension Markdown All In One looks like it does what you want in one step.

Paste link on selected text

paste link demo

Just select your link and hit Ctrl+V and it creates the link and inserts the clipboard link.

If for some reason you don't want to use this extension, it would be pretty easy to create a snippet to do what you want.

Sign up to request clarification or add additional context in comments.

5 Comments

Heyyy, that's very nice and even shorter! How did you find out about that? It's not in the keyboard shortcuts tabel in the description of the extension! – I like especially that it intelligently checks whether the clipboard content is a URL and only then does this magic. Great!
Ah, found the aGIF you posted above in the extension's details! 🙂
Markdown All In One is a good extension but sometimes interferes with my workflow: I wish there was a way to have the shortcut feature alone and not the rest. VsCode supports Markdown in many ways, I wonder why they have not implemented this basic thing natively.
This is perfect, just what I was looking for. Thanks, buddy
What if I want to use Ctrl+K instead Ctrl+V? Because I thinks that's a quite standard way to do it in many applications. Is there any option in the extension? I don't see it :/ but thanks for your answer, it was useful !
15

Adding another answer that doesn't use the extension Markdown All In One I mentioned in the other answer and because a couple of commenters requested a different way. @MarcoLackovic

  • First Method: keybinding in keybindings.json, then manually paste
{
  "key": "alt+w",                  // use whatever keybinding you wish
  "command": "editor.action.insertSnippet",
  "args": {
    "snippet": "[${TM_SELECTED_TEXT}]($0)"
  },
  "when": "editorHasSelection && editorLangId == markdown "
}

Select the link text and, trigger your keybinding - the cursor will be placed where you want it and paste.


  • Second Method: use a macro to insert the snippet and paste in one step

You will need a macro extension like multi-command to run multiple commands in series. Then this keybinding:

{
  "key": "alt+w",
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      {
        "command": "editor.action.insertSnippet",
        "args": {
          "snippet": "[${TM_SELECTED_TEXT}]($0)"
        }
      },
      "editor.action.clipboardPasteAction"
    ]
  },
  "when": "editorHasSelection && editorLangId == markdown "
}

Demo of second method:

demo of markdown link generation

3 Comments

Super, Mark, that works fine! 🙂 Thanks a lot!
how do we give the snippet a name?
@klewis You can create a snippet in one of your usual snippets files and then use that name in the above keybindings. The arg would look like "name": "someNameFromSnippetsFile".
9

The latest release of VSCode in January 2024 (1.86.0) now supports this as a standard feature. See the release notes:

Want to turn that link you copied into a Markdown link? When you paste a URL into a Markdown file with text selected, VS Code now automatically inserts a Markdown link

vscode markdown link paste feature demo

Comments

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.