Vim Keymap For Markdown Links

I work with Markdown a lot. I use it for note-taking, documenting projects, creating personal wikis (and a world-building wiki!), etc. And most recently, I have started this blog so I use it even more than before.

Whenever I edit text in Markdown, I often find myself adding links here and there. Since I also use Neovim as my primary text/code editor, I can easily add links to text elements using minimal vim motions.

It works well and it literally takes less than a second to do that. But when I have to add dozens of links, it starts feeling a bit repetitive.

The great thing about Vim/Neovim: You can configure your editor to do EXACTLY what you want. So, I thought: "why not create a custom keymap for that?"

What I wanted:

  1. Copy some URL to my system clipboard
  2. Select some text in Neovim
  3. Type <leader>l
  4. Have Neovim convert the selection to a markdown link [text](link), where:
    • text is the selected text
    • link is the URL I copied to my system clipboard (+ register in Vim)

The keymap

Here's the keymap I have added to my Neovim config:

vim.keymap.set(
    "x",
    "<leader>l",
    "<Esc>`<i[<Esc>`>la](<C-r>+)<Esc>",
    { desc = "Apply markdown [L]ink on current selection" }
)

To define a new keymap, we call vim.keymap.set(...) and give it some arguments:

The thing we want to have a look at is the sequence of motions:

<Esc>`<i[<Esc>`>la](<C-r>+)<Esc>

So, let's break it down:

That was not so bad, was it?

Complementary keymap for Normal mode

The previous keymap works great for any visual selection. It allows you to select some text and apply a markdown link to it.

But what if you only want to add a link to a single word? Do we really want to bother with a visual selection for that? I don't think so. Time to create a second keymap:

vim.keymap.set(
    "n",
    "<leader>l",
    '"9ciw[]<Esc>"9Pf]a(<C-r>+)<Esc>',
    { desc = "Apply markdown [L]ink on current word" }
)

It looks very similar to our first keymap with a few tweaks here and there. The first difference we notice is the first argument "n" which tells Neovim to enable this keymap in Normal mode only. The key binding has not changed, we still want to use <leader>l.

This means both keymaps use the same key binding, the only difference is the Vim mode you are in. This is what determines which keymap to call.

Okay, let's go over the motions now:

"9ciw[]<Esc>"9Pf]a(<C-r>+)<Esc>

If you pay a closer look to the last part, you will notice that it is the same as before:

a](<C-r>+)<Esc>

So the only motions that differ are the ones prior to inserting the closing square bracket ']':

"9ciw[]<Esc>"9Pf]

New motions to break down. Exciting!

So now, if we move the cursor on any word in Normal mode and hit <leader>l, it will automatically format it as a Markdown link. Pretty cool!

Conclusion

Okay, this might seem like a lot just to save a few milliseconds here and there. Was it really worth it?

YES! (I have used <leader>l a lot to write this article already)

Maybe it will take a lot of time before this keybind actually pays off. But overall, I am having so much fun just trying to solve these kind of problems in Neovim. This is what makes using Neovim so enjoyable to me.

I think having fun is the best remedy to avoid burnouts. For me, I get my fun from using Neovim and the terminal, customizing my environment to be exactly how I want it. It's probably different for you but I hope you get your fun from somewhere else at least.

I hope I made you learn some neat Vim tricks! If you liked this article, please consider sharing it around to any other vim addicts. If you want to take a peek at my Neovim config, I keep it on GitHub: coko7/neovim-config

Feel free to check out my other articles too! Until next time 👋

:wqa