I have a bash script which Sweaves .Rnw files into pdf's.

sweave_to_pdf ~/Foo.Rnw

I am wanting to write a emacs function to invoke this script on the current buffer. So far I have this:

(defun sweave_to_pdf ()
  "sweave_to_pdf function"
"~/Scripts/Shell/sweave_to_pdf.sh" (buffer-file-name (window-buffer (minibuffer-selected-window))))

It seems the file name and path of the buffer is not being passed to the shell script as a line argument. Any ideas how I could achieve this?

2 Answers 2


The invocation needs to be a single string as the second and third argument refer to the output and error buffer:

(defun sweave-to-pdf ()
  "sweave_to_pdf function"
  (shell-command (format "%s %s" "~/Scripts/Shell/sweave_to_pdf.sh"
  • I'd add some (shell-quote-argument) magic around buffer-file-name to avoid problems with spaces and such.
    – YoungFrog
    Jul 22, 2015 at 15:48
  • Worked perfectly. Jul 22, 2015 at 15:54
  1. As YoungFrog mentions, you must quote the file name. Otherwise, the code fails if, for instance, the file name contains whitespaces.

  2. You might explicitely check that the current buffer is visiting a file.

(defun sweave-to-pdf ()
  "Export a sweave file to pdf."
  (let ((file buffer-file-name)
        (script "~/Scripts/Shell/sweave_to_pdf.sh"))
    (unless file (user-error "Buffer must be visiting a file"))
    (shell-command (format "%s %s" script (shell-quote-argument file)))))

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.