Thomas Skardal

Hello, computer.

Configuring Emacs from scratch

April 30, 2015

I’ve been using Emacs for quite some while now. I’ve been using others setup, and I’ve smashed together my own setups. And now, after using ohai-emacs for a while, I’ve decided to throw it all out and build it up from scratch again.

First of all; I’m no Emacs expert. Not at all! By doing this I hope to learn more about Emacs and the Emacs Lisp dialect. I’ll take small steps and try to explain why and how. This is the first (and hopefully not last) post about Emacs customization.

My goals for this part is to create.

init.el

This is where it all begins. When you start Emacs, this file is your entry point. Let’s have a look at it.

;; Setup load-path
(setq elisp-dir (expand-file-name "elisp" user-emacs-directory))
(add-to-list 'load-path elisp-dir)

;; No splash screen
(setq inhibit-startup-message t)

(require 'setup-package)
(require 'better-defaults)
(require 'setup-mac)

;; I want path from shell on both MacOS and GNU\Linux
(package-require 'exec-path-from-shell)
(exec-path-from-shell-initialize)

The first thing I need to do is to add make my extensions and customizations available. I have to add a folder to the load-path.

To see what your current load-path is you can use the command C-h v. This will also give you a description of the variable if available.

I’ve added the folder ‘elisp’ inside my emacs directory. This name is probably not permanent, but it’s good enough for now. This is where I’ll put most of my code.

(setq inhibit-startup-message t) sets the value of this variable to t which can be considered a truthy value. If this variable is set to a non-nil value the splash screen will not be shown. Again, look up both of these variables with the help functions inside Emacs!

Next step is the exec-path variable. If you start Emacs from elsewhere than your shell you probably want this. It takes the $PATH defined in your shell and adds it to exec-path which is Emacs’s $PATH. Without it you will for instance have trouble running cider-jack-in if you use Emacs for Clojure development.

The next three lines import code provided by the given names. Better defaults are what the names say. Better defaults. This is just downloaded and placed into the elisp folder (which we added to load-path, remember?). Next up is setup of packages. Emacs extensions are distributed as packages. Some are found in the official Emacs repository (ELPA), but most users also add MELPA or Marmalade (or both) to their list of archives.

setup-package.el

(require 'package)

(add-to-list 'package-archives
	     '("melpa" . "http://melpa.org/packages/") t)

(package-initialize)

(defun package-require (pkg)
  "Install the package if it's not already installed"
  (when (not (package-installed-p pkg))
    (package-install pkg)))

(provide 'setup-package)

Not much going on here. As mentioned I’ve added MELPA so that I can install packages from this package repository. The interesting thing here is the package-require function. This one is yanked from ohai-emacs and let us require packages and automatically install them if not already installed. I think the code is pretty self explanatory. Use C-h f to look up documentation for each of the functions if it doesn’t make sense to you.

Finally we provide our functions and setup code as a feature named setup-package. This is necessary in order for us to be able to require it later on.

setup-mac.el

(setq is-mac (equal system-type 'darwin))

(when is-mac
  ;; change command to meta, and ignore option to use weird Norwegian keyboard
  (setq mac-option-modifier 'super)
  (setq mac-command-modifier 'meta)
  (setq ns-function-modifier 'hyper)

  ;; Norwegian mac-keyboard alt-keys)
  (define-key key-translation-map (kbd "s-8") (kbd "["))
  (define-key key-translation-map (kbd "s-(") (kbd "{"))
  (define-key key-translation-map (kbd "s-9") (kbd "]"))
  (define-key key-translation-map (kbd "s-)") (kbd "}"))
  (define-key key-translation-map (kbd "s-7") (kbd "|"))
  (define-key key-translation-map (kbd "s-/") (kbd "\\"))
  (define-key key-translation-map (kbd "M-s-7") (kbd "M-|")))

(provide 'setup-mac)

These are customizations specific to mac users. Currently this means keyboard setup. These lines are copied from Magnar Sveen’s setup.

And that’s it!