# Working Markdown links

**URL:** <https://discuss.kakoune.com/t/working-markdown-links/2039>\
**Category:** Help\
**Created:** [April 9, 2022, 10:33am UTC](https://discuss.kakoune.com/t/working-markdown-links/2039 "2022-04-09T10:33:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bosko](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/b/b77776/32.png) [@bosko](https://discuss.kakoune.com/u/bosko)\
**Post date:** [April 9, 2022, 10:33am UTC](https://discuss.kakoune.com/t/working-markdown-links/2039/1 "2022-04-09T10:33:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Screwtapello](https://discuss.kakoune.com/user_avatar/discuss.kakoune.com/screwtapello/32/12_2.png) [@Screwtapello](https://discuss.kakoune.com/u/Screwtapello)\
**Post date:** [April 9, 2022, 12:55pm UTC](https://discuss.kakoune.com/t/working-markdown-links/2039/2 "2022-04-09T12:55:03Z")

</div>

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:

```auto
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](https://zork.net/~st/jottings/Intro_to_Kakoune_highlighters.html).

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.

---

<div class="post-metadata">

**Author:** ![bosko](https://discuss.kakoune.com/letter_avatar_proxy/v4/letter/b/b77776/32.png) [@bosko](https://discuss.kakoune.com/u/bosko)\
**Post date:** [April 9, 2022, 1:55pm UTC](https://discuss.kakoune.com/t/working-markdown-links/2039/3 "2022-04-09T13:55:59Z")

</div>

Thank you for the tips. Exactly what I wanted.
