I'm trying to write a simple little function to follow markdown wiki links of the form [[wiki link]]. I have a bunch of markdown notes with links of this type and they all link to files with the .md extension. The function would look something like this:

function follow-wiki-link()
  vim.cmd('e ' .. filename .. '.md')

Im planning to bind the function to some key like gl. So the problem lies in getting the filename under the cursor. Is there any way to get the text that is between double braces and under the cursor in Lua?

[[text to get]]
            ^cursor is here, for example


  • 3
    I highly suggest using 'path' and gf (lots of ways to control gf linked at its help, like 'isfname', 'suffixesadd', 'includeexpr') to make this work: I do this for my personal wiki plugin. You'll get a lot more mileage out of builtins.
    – D. Ben Knoble
    Dec 19, 2023 at 14:17
  • I'll look into those options thanks for mentioning them.
    – ln4lar
    Dec 19, 2023 at 14:40


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.