2

I have created a table of contents at the beginning of my markdown document and linked the headers of the document:

# TOC
[first header](#first-header)

# first header

Now, having opened the document in vim, I want to jump from the reference in the table of contents directly to the respective header. What is the keystroke combination? I found out that with gf I can jump to linked files, but for my case everything is in the same document.

4
  • 3
    There is no such command in vim. You can create it yourself though.
    – Maxim Kim
    Nov 8 '21 at 7:31
  • If you want even easier navigation I have a plugin that shows all sections in a popup and let's you jump to the selected section header. However, I have yet to advertise it (i.e. it's not widely shared) and the Markdown support is new and not heavily tested (I wrote it for Asciidoc originally) so you'd kind of be a beta tester. ;) Still, if you're interested: github.com/b-layer/musecnav (currently Vim only since Neovim has totally different "popups")
    – B Layer
    Nov 8 '21 at 13:57
  • 1
    Relatedly, a simple section jumper is :vimgrep /^#/ %; I've customized mine a bit more here and here
    – D. Ben Knoble
    Nov 8 '21 at 15:31
  • Thank you @D.BenKnoble, my workaround is something similar, such a sequential search gets just a bit cumbersome with a lot of headers.
    – AKG
    Nov 10 '21 at 7:24
3

I'm not aware of any built in vim functionality that works like this, but here are 2 options:

  • Use the link text to search for the heading. e.g. vi[y/<c-r>0<cr>

    • vi[ visually select inner square brackets
    • y copy the selection
    • / start a search
    • <c-r>0 to access register 0 (the most recent yank)
    • <cr> Enter
  • Use the vim-markdown plugin. It allows you to follow named anchors with the ge command. See here -> https://github.com/plasticboy/vim-markdown#follow-named-anchors

2
  • Thank you for the reply. The first suggestion only works if there are no other occurrences of that specific word in the document. I cannot get the second suggestion to run. If I understand the syntax correctly, if one wants to search in the same file, one writes instead of file#anchor only #anchor and sets let g:vim_markdown_follow_anchor = 1, navigates to the anchor and hits ge. For me, it is just jumping a few characters backwards. What am I doing wrong?
    – AKG
    Nov 10 '21 at 7:21
  • It sounds like you’re doing it correctly, maybe reach out to the library maintainers on GitHub for help. You could also look through some GitHub issues to see if others have had the same problem Nov 12 '21 at 0:34
2

One maybe even more elegant solution than manually generating a table of contents is to use the :Toc functionality from the vim-markdown plugin (see https://github.com/plasticboy/vim-markdown#commands). This function opens a new window with a navigatable table of contents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.