This can save you a few seconds (and some mild annoyance) per day if you write .md files or markdown cells in Jupyter-like notebooks with VS Code or its forks; otherwise, you can safely skip reading it:

When you are editing some markdown text with VS Code & Co., you cannot select text and paste a URL to create [selected text](URL). This makes adding links just annoying enough that I finally had to fix it, and the solution turned out to be rather simple: You just need to add the following rule

	{
		"key": "ctrl+v",
		"command": "editor.action.insertSnippet",
		"when": "editorTextFocus && editorHasSelection && editorLangId == markdown",
		"args": {
			"snippet": "[$TM_SELECTED_TEXT]($CLIPBOARD)"
		}
	}

in your keybindings.json.1

My keybindings.json is as follows after the update and it works:

[
	{
		// ... other keybindings ...
	},
	{
		"key": "ctrl+v",
		"command": "editor.action.insertSnippet",
		"when": "editorTextFocus && editorHasSelection && editorLangId == markdown",
		"args": {
			"snippet": "[$TM_SELECTED_TEXT]($CLIPBOARD)"
		}
	}
] 

This is how it looks like when in use:

alt text

Outro

A caveat: If you are used to this feature from other platforms (e.g. Github), you might expect it to validate the link before doing its thing so that if you paste “I thought this string was a link but I was wrong”, it won’t create [selected text](I thought this string was a link but I was wrong). This is not the case with the above fix so you need to check that you copied a link, which is not that difficult since you see it immediately anyway.2

Another option: In the above rule, I used ctrl+v as the trigger. You could, of course, use any other key by simply adjusting the key line accordingly, for instance, ctrl+shift+v. You might want to do this if you are used to overwriting an existing text by selecting it and pasting its replacement.

Inspiration for the post: I realized how annoying this link pasting issue was after watching this video by Vincent D. Warmerdam. I liked the idea of writing my own editor, and I might eventually give it a go3 but this one felt like such an easy quality of life improvement that I decided to add this to VS Code & Co. for the time being. Vincent says that he wants people to blog more; even though I haven’t written my own editor yet, I changed my workflow for the better, and blogged about it so it worked!

Illustration

Some local link that won’t work for you


  1. With VS Code or Cursor or another fork open, press “ctrl + shift + p” and then start typing “Preferences: Open Keyboard Shortcuts: JSON” and select this option to be taken to keybindings.json↩︎

  2. We could technically write a short extension that essentially validates the copied text as “link-like” but I don’t feel like maintaining an extension to solve this issue. ↩︎

  3. Maybe I should say, I might eventually Go for it. Seriously though, I have been meaning to learn Go, and this could be my learning project. Also, do you see how easy it is to add links now? You don’t see that but you can easily count the links per sentence in this post, and compare it against my other posts. ↩︎