Using sudo in org-babel

Here's a simple way of using sudo in org-babel:

#+NAME: just-open
#+BEGIN_SRC shell :tangle no :dir /sudo::~mark/writing
exec 2>&1  
mkdir -p perf && cd perf  
/home/mark/.guix-profile/bin/perf stat -e 'block:*' ../open-perf
#+END_SRC
  • Use :tangle no to exlude this snippet from being tangled
  • Use :dir /sudo::~mark/writing to activate TRAMP (/sudo) and set the current path that is to be used. Note: paths relative to the document didn't work out for me
  • Use exec 2>& to redirect stderr to stdout to capture the entire output of your command, not just stdout
  • Finally use the full path to the command, because your user environment may be different then the sudo environment you're running in.

That's it. Tested on org-mode 9 and emacs 25.3.

comments powered by Disqus