Working Markdown links

I decided to use Kakoune as a Markdown editor. I would like to have that if there are file and website links that they would be opened by for example having a main cursor on it and then some keybinding. Files are opened in Kakoune and websites are opened in a browser. Also I whould like to have them underlined. I am not only doing this because I want to achieve that but to practice Kakoune configuration. I did not find a lot of helpful info in the Github wiki for this. Also I do not want to use any other editor for markdown because of superior Kakoune keybindings and more modern TUI management.

If you just want to practice Kakoune configuration, let me give you some tips:

If you want to make something happen when a keybinding is pressed, you’ll want a mapping, so see :doc mapping

Probably you don’t want the mapping to always be active, just in Markdown files. You can tell which buffers are Markdown files by hooking the WinSetOption filetype=markdown event, see :doc hooks for details.

When your keybinding is pressed, you’ll want to do a whole bunch of things to detect different types of links and decide what to do with them. While it’s theoretically possible to put all that into a mapping, your life will be easier if you make your mapping execute a custom command. See :doc commands declaring-new-commands for details.

Inside your command, you’ll want to try to detect what kind of link the cursor is in. It might be in either part of the full inline [text](target) syntax, or the footnote style [text] syntax with [text]: target somewhere else. For this, the usual idiom is:

  • inside a try %{} block, try to select the most specific context, using execute-keys to execute normal-mode commands
  • most normal-mode commands for selecting a regex will fail if the selection doesn’t match, so Kakoune will jump out of the try block to the following catch block
  • inside the catch %{} block, try to select the next-most-specific context
  • if that fails, Kakoune will jump out to the next catch block, and so forth

At a high level, it might look something like this:

try %{
    # Are we in the target section of an inline link?
    # Try to select the surrounding round brackets
    execute-keys <a-i>(
    # Assert that there's a square bracket before this round-bracketed text
    execute-keys s(?<=]\()\A.*\z(?=\))<ret>

    # We got this far, so the selected text is probably a link target
} catch %{
    # Check if we're in some other kind of link
} catch %{
    # Some third kind of check

If you’re going to be messing with the selection like that, you’ll probably want to wrap the entire thing in evaluate-commands -draft %{} so that your command doesn’t mess with the user’s selection. See :doc execeval for details.

Once you have the link target selected, you have to figure out what to do with it. Probably the easiest thing would be to check if the link target begins with https?:// or not, using the same kind of try %{} catch %{} chain as above.

For links that look like URLs, you can probably palm them off to a helper like xdg-open that knows about default browsers and the like. To launch a shell command from inside Kakoune, the idiom is nop %sh{ whatever } - the %sh{} block is executed as a shell command, and the result is passed as a parameter to the nop command, which does nothing and ignores its arguments.

For links that look like local files, you’ll need to build a Kakoune command based on the absolute path of the current buffer, plus the link target text. You can get the absolute filename of the current buffer with %val{buffile} (see :doc expansions value-expansions) and the contents of the selection with %val{selection}. There’s easy way to get the directory containing the current file from inside Kakoune, but it’s easy enough in a shell-script. :doc expansions shell-expansions will tell you how what Kakoune’s expansions look like inside shell-scripts, and I’ll provide the extra hint that the shell syntax ${somevar%/*} will expand to the value of $somevar with the last /-separated segment removed.

If you want to underline things that look like links, that’s a whole separate skill set. You’ll want to check out :doc highlighters, and maybe also my own Intro to Kakoune Highlighters.

That’s a whole lot of information, and it’s probably not as helpful as you’d hoped. If you have followup questions, feel free to ask - there’s a lot of talented Kakoune users around here.


Thank you for the tips. Exactly what I wanted.