Install Emacs, ESS and org-mod on OSX


For this tutorial, you will need to have previously installed brew.

Step 1 : Install Emacs

Emacs is both a terminal and a desktop application. By default, the brew formula will only install the terminal version. But if you wish to have the desktop one as well, you just need to add the --with-cocoa flag in the installation command.

To install Emacs, just launch these commands.

$ brew update
$ brew install emacs [--with-cocoa]

If you installed the desktop version, as of right now it can only be started using a command

$ /usr/local/Cellar/emacs/24.5/bin/emacs

To start emacs using the launchpad you will first need to link the application.

$ brew linkapps emacs

Emacs will then quickly appear in the launchpad.

Step 2 : Install ESS

Once again, we will use brew to install this. The ESS formula is however not found in the default brew formulas. Just like R, it exists in the science formula. (Install R On OSX).

If you haven't used the formula yet, you will most likely need to tap it.

$ brew tap homebrew/science

Retrieving ESS is then easy.

$ brew install ess

To have ESS running in Emacs, the following must then be added to your ~/.emacs file.

;; Loading ESS
(require 'ess-site)

Step 3 : Configure $PATH in emacs

If you haven't installed the desktop version of emacs (with the --with-cocoa flag), you can skip this step.

The org-mode uses the environment variable $PATH to look for programs and execute blocks.

When you start Emacs from inside a terminal, the $PATH variable will be copied from your shell to the app. It will thus contain all modifications you applied to it since the creation of the shell. But if you start it from the launchpad, the $PATH variable of Emacs will be the system's minimal one. Since it won't load your shell configuration file, you won't have access to any soft you installed outside of /bin or /usr/bin.

The terminal version of emacs will always have the right value of $PATH. The problem lies in how to set the value for the windowed version. But don't worry, I found a trick on stackOverFlow

To use it, update your ~/.emacs file and add the following :

;; Use the value of $PATH constructed at shell's creation
;; (done only when emacs started with window-system)
(defun set-exec-path-from-shell-PATH ()
    (let ((path-from-shell (replace-regexp-in-string
                                "[ \t\n]*$"
                (shell-command-to-string "$SHELL --login -i -c 'echo $PATH'"))))
                (setenv "PATH" path-from-shell)
                (setq eshell-path-env path-from-shell) ; for eshell users
                (setq exec-path (split-string path-from-shell path-separator))
(when window-system (set-exec-path-from-shell-PATH))

The idea is pretty simple. When the windowed version of Emacs is created, a shell is started and its value of $PATH is retrieved. This value is then simply set to the current environment.

You might have read from the stack overflow answer that a library exists to do this. Using the library might be better and it even is found on several repository. I decided not to use it because it seems to be doing a lot while I only need this small

Step 3 : Active org-mode

Once again, this is done by update your ~/.emacs file.
Add the following :

;; Load org
(require 'org-install)
(require 'org)

;; Supported org languages
        (sh . t)
        (python . t)
        (R . t)
        (org . t)
(setq org-src-preserve-indentation t)

In this version, sh, python, R and org will be allowed to be executed. For security reasons, any org blocks with a different language will not be runnable. If you ever wish to add a language (perl for example), just add an entry in this list.

Step 4 : test org-mode

Now is the the time to test org. Open a new instance of Emacs and open a .org file. Copy in this file the following

#+begin_src sh :session foo :results output :exports both
ls -la  

To execute the code, you must use the following command : C-c C-c (If you are not familiar with Emacs commands, this one means 'Ctrl + C' twice)

A Result block with the result of the command should appear if it worked.

Sources :

Comments powered by Disqus