0

My workflow is to perform data analysis in R markdown files. They are a mix of text and code chunks, pretty much like jupyter notebooks for python. These files can get long with time and I like to open a buffer with a table of content to navigate between the various headers of the document. I was very happy to use the vim-markdown plugin to generate table of contents for markdown files. In this question, I managed to create an auto command to activate vim-markdown for Rmd files. But now that I start using Nvim-R more intensively, I hit vim-markdown issue 263: activating vim-markdown removes syntax highlighting and auto completion of the R code chunks. That issue hasn't been resolved since 2016, so I guess it will be hard to make both plugins work in parallel.

I'm only interested in the table of content functionality, is there another way to display a table of content on a buffer?

My previous hack was to call

:g/^#

To display a list of markdown headers. I sill miss the extra buffer navigation functionality though.

3
  • B-layer just released a plugin called musecnav for navigating markup—I’m sure contributions adding support for R-markdown would be appreciated! – D. Ben Knoble Jun 23 '20 at 11:34
  • @D.BenKnoble can you please share a link to that "musecnav" plugin? – Paul Rougieux Jun 23 '20 at 12:42
  • 1
1

You could use vimgrep and the quickfix window.

Define a command, that searches for headers and open a quickfix window:

command T vimgrep '^\#' % | cw

The defines the command T that first greps the current file for all lines starting with a # and then opens a quickfix window for it.

To execute it, just run :T. Then move to the heading you are interested in and hit <Enter>.

See :h :vimgrep, :h :cw and .

If you want to dive deeper, also have a look at :h lvimgrep, h :lw and :h location-list.

0

There is an effective solution using plugins. You can achieve a TOC, which you can fuzzy search in, using CtrlP and ctrlp-funky.

Start by installing:

Plug 'ctrlpvim/ctrlp.vim'
Plug 'tacahiroy/ctrlp-funky'

Then you can attach CtrlPFunkyMulti to a key combination.

2
  • Welcome to Vi and Vim! After cutting out the irrelevant bits (your config, your having the same question, and the niceties which we generally omit here), the answer boils down to "install plugin <link>". It would be nice if you edit your answer to remove the bits I indicated and instead flesh out how to use the plugin, possibly with examples. This allows us to judge the answer as it stands, independently of where it links to or any supporting docs. Such links and docs are of course nice, too, but are tertiary. – D. Ben Knoble Jun 25 at 18:24
  • Thanks, see changes. – lf_araujo Jun 25 at 19:19
0

Voom can create a table of content for R markdown files if you associate that file type to pandoc in the vim configuration file:

let voom_ft_modes = {'markdown': 'pandoc', 'rmd': 'pandoc'}

Then you can also add another name for the command:

au BufRead,BufNewFile *.Rmd command! Toc Voom

Calling :Toc in an R markdown file will now display an outline pane in a vim buffer to the left of your screen.

Thanks to jalvesaq for suggesting voom and this setup, as visible in the nvim-r documentation.

1
  • One year later, Voom remains my preferred tool to manage the table of content of all markdown files, including R markdown. The best feature is the possibility to reorder sections of the document from the TOC pane. – Paul Rougieux Jun 28 at 10:30

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.