Search code examples
vimmarkdownpandocctagstagbar

Vim markdown navigation with ctags: using 'tagbar' + 'markdown2ctags' with 'vim-pandoc'


All,

I'd like to be able to use the Vim Tagbar plugin with markdown2ctags to navigate my markdown files.

I haven't been able to automatically generate the tags file because the filetype, per :set filetype? is currently set to pandoc. That's becaue I have the the vim-pandoc plugin installed. However, if I change the filtype to markdown everything works.

I have control over two items: my ~/.vimrc file and my ~/.ctags file. Does anyone have an idea of how things should be set? I've tried changing markdown to pandoc in the excerpts below, but that didn't help.

Edit: As per the comment by @IngoKarkat, because markdown2ctags is handling the creation of my tags file, the .ctags settings are irrelevant.

From my ~/.vimrc file... (set as per the Tagbar instructions for markdown files)

let g:tagbar_type_markdown = {
    \ 'ctagstype': 'markdown',
    \ 'ctagsbin' : '~/.vim/plugged/markdown2ctags/markdown2ctags.py',
    \ 'ctagsargs' : '-f - --sort=yes --sro=»',
    \ 'kinds' : [
        \ 's:sections',
        \ 'i:images'
    \ ],
    \ 'sro' : '»',
    \ 'kind2scope' : {
        \ 's' : 'section',
    \ },
    \ 'sort': 0
\ }

...and my ~/.ctags file (Edit: Unused, but still including)

--langdef=markdown
--langmap=markdown:.mkd
--regex-markdown=/^#[ \t]+(.*)/\1/h,Heading_L1/
--regex-markdown=/^##[ \t]+(.*)/\1/i,Heading_L2/
--regex-markdown=/^###[ \t]+(.*)/\1/k,Heading_L3/

Thanks,

Sean


Solution

  • I found a few solutions to the problem. I've listed them here in case someone finds them useful.

    Solution 1: Re-define the pandoc filetype to the markdown filetype

    I looked at the vim-pandoc pandoc.txt file and the solution was there. The relevant information:

    To enable pandoc functionality for markdown files while using  the markdown
    filetype and syntax, use
    >
         let g:pandoc#filetypes#handled = ["pandoc", "markdown"]
         let g:pandoc#filetypes#pandoc_markdown = 0
    
    Note: vim-pandoc's developers mostly use pandoc's markdown syntax, so
    coverage for it is more complete than for the other filetypes.
    

    With the filetype now correctly set, everything works. The ~/.vimrc should be (with appropriate substitution for your own ctagsbin path):

    let g:pandoc#filetypes#handled = ["pandoc", "markdown"]
    let g:pandoc#filetypes#pandoc_markdown = 0
    
    let g:tagbar_type_markdown = {
        \ 'ctagstype': 'markdown',
        \ 'ctagsbin' : '~/.vim/plugged/markdown2ctags/markdown2ctags.py',
        \ 'ctagsargs' : '-f - --sort=yes --sro=»',
        \ 'kinds' : [
            \ 's:sections',
            \ 'i:images'
        \ ],
        \ 'sro' : '»',
        \ 'kind2scope' : {
            \ 's' : 'section',
        \ },
        \ 'sort': 0
    \ }
    

    Solution 2: Define a new pandoc tagbar type

    Add the following to your ~/.vimrc

    let g:tagbar_type_pandoc = {
        \ 'ctagstype': 'pandoc',
        \ 'ctagsbin' : '~/.vim/plugged/markdown2ctags/markdown2ctags.py',
        \ 'ctagsargs' : '-f - --sort=yes --sro=»',
        \ 'kinds' : [
            \ 's:sections',
            \ 'i:images'
        \ ],
        \ 'sro' : '»',
        \ 'kind2scope' : {
            \ 's' : 'section',
        \ },
        \ 'sort': 0
    \ }
    

    Everything should work! This is my preferred solution.

    Solution 3: Use the builtin pandoc :TOC command.

    The vim-pandoc :TOC command produces a navigational menu, however, I don't like it as well as the sidebar tagbar menus. But it is a built in default.