0

I am trying to autogenerate an output every time I save the file with :update or with :w. This is what I got so far:

augroup renderRmd | au!
    autocmd BufWritePost *.rmd !'Rscript -e "rmarkdown::render(<afile>:p:S)"'
augroup end

But I get:

:!'Rscript -e "rmarkdown::render('/home/luis/Documents/2020-CamCAN/src/2021-09-1
8-time-series.rmd')"'
zsh:1: no such file or directory: Rscript -e "rmarkdown::render(/home/luis/Docum
ents/2020-CamCAN/src/2021-09-18-time-series.rmd)"

Running Rscript -e "rmarkdown::render('/home/luis/Documents/2020-CamCAN/src/2021-09-1 8-time-series.rmd')" in zsh works as expected. I feel like this is something with the expansion of the name, but I can´t figure it out.

Also, it does not work with :update, only with :w

extra points if this is run asynchronously from Vim, or some kind of run in the background.

7
  • For starters, get rid of the single quotes. And focus first on getting your shell command to run directly from the Vim command line and then worry about the autocommand. As far as backgrounding goes what do you use from the shell to do that? Did you try that from Vim?
    – B Layer
    Commented Sep 19, 2021 at 4:08
  • :terminal or tpope’s dispatch plugin could run asynchronously.
    – D. Ben Knoble
    Commented Sep 19, 2021 at 11:34
  • Does the full parent path ( "~/Documents/2020-CamCAN/src" ) exist? If you are trying to save a file to a sspecific path, the parent path must already be there.
    – Nate T
    Commented Sep 19, 2021 at 12:39
  • It does exist @nate, I found a non sync solution, which I will post in some minutes.
    – lf_araujo
    Commented Sep 19, 2021 at 12:57
  • @NateT The file has to exist because it's the file being saved...that's what <afile> is. :) The error message is looking at the whole command line as a file, though....that's what the error is. It's from the single quotes.
    – B Layer
    Commented Sep 19, 2021 at 12:57

2 Answers 2

0

With https://github.com/tpope/vim-dispatch installed, I would do something like the following

" with appropriate augroup, of course
autocmd BufWritePost *.Rmd Dispatch Rscript -e "rmarkdown::render(<afile>:p:S)"

You could use other commands from that plugin depending on how background/foreground you want the command.

1

Although an async alternative would be better, this one is pretty neat. The problem as comments pointed out was the quotes.

augroup renderRmd | au!
    autocmd BufWritePost *.Rmd call Render()
  augroup end


function! Render() abort
  :tabnew | te Rscript -e "rmarkdown::render(<afile>:p:S)"
  :tabprev
endfunction

This works with :w and generates the output correctly, while leaving the terminal in a tab/buffer in the background.

1
  • 1
    I'm pretty sure the first line can be simplified to :tab terminal Rscript …
    – D. Ben Knoble
    Commented Sep 22, 2021 at 20:09

Your Answer

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

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