POSTS

Making org-babel shell work on windows

Using org-babel on windows can be a bit tricky as bash and other shells often don’t work quite right. Advice can help.

Even if you have installed cygwin and configured your shell correctly, you still may have subtle problems. For example, the following uses shell continuations and should produce exactly one line of output:

echo this group of \
  echo statements should be \
  echo all on one line

On some windows systems, however, you will get three lines of output because emacs puts carriage returns after the backslashes and so line continuation breaks.

As various posts on emacs.stackexchange.com describe, you can use the advice features of emacs to fix this:

  (defadvice org-babel-sh-evaluate (around set-shell activate)
    "Add header argument :file-coding that sets the buffer-file-coding-system.
You can provide something like `:file-coding utf-8-unix` as a header to
your BEGIN_SRC block to force a particular coding system when passing
information to the shell for evaluation."
    (let ((file-coding-param (cdr (assoc :file-coding params))))
      (if file-coding-param  ;; checks if :file-coding param provided
	  (let ((file-coding (intern file-coding-param))
		(default-file-coding (default-value 'buffer-file-coding-system)))
	    (setq-default buffer-file-coding-system file-coding)
	    ad-do-it ;; original function gets dropped in here w/ file-coding
	    (setq-default buffer-file-coding-system default-file-coding)
	    )
	ad-do-it  ;; original function dropped in here w/o file-coding
	)
      )
    )

Once you have executed the emacs-lisp code above, you should be able to use an org-babel shell block and provide :file-coding utf-8-unix with the code below

#+BEGIN_SRC shell :file-coding utf-8-unix
echo this group of \
  echo statements should be \
  echo all on one line
#+END_SRC

and get the results to show up as desired.