Michael's Emacs Config

1 Preamble

This is my Emacs config, which is almost a single file thanks to org-babel.

Although GitHub does render .org files nicely, I recommend viewing it in Emacs, as GitHub isn't fully compatible. Using org-mode gives a nice overview and makes it quick and easy to navigate and make changes.

2 Beginning config

2.1 Personal information

(setq user-full-name "Michael Englehorn")
(setq user-mail-address "michael@englehorn.com")

3 Package related

Setup general package related settings.

3.1 package.el

Initialize package.el, the built-in package manager.


These are the package repositories I use:

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

3.2 use-package

Install/activate use-package, basically a wrapper for package.el.

  • makes installing and configuring other packages nicer and easier.
  • helps with autoloading packages to greatly reduce initial startup time.
    (unless (package-installed-p 'use-package)
        (package-install 'use-package))
    (require 'use-package)
    (setq use-package-always-ensure t)

3.3 Emacs String Manipulation Library

This is required for some plugins.

(use-package s)

4 Appearance related

Setup appearance early so that if something goes wrong with the init, Emacs is still looking good.

4.1 Maximize Emacs on startup

This snippet works in *nix and all (relevant) versions of Windows.

(if (eq system-type 'windows-nt)
      (defun w32-maximize-frame ()
        "Maximize the current frame (windows only)"
        (w32-send-sys-command 61488))
      (add-hook 'windows-setup-hook 'w32-maximize-frame t))
  (set-frame-parameter nil 'fullscreen 'maximized))

4.2 Minimal GUI

Remove unnecessary GUI elements: menu-bar, tool-bar, and scroll-bar.

(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))

4.3 No splash screen

I prefer loading up to the scratch buffer on initial startup.

(setq inhibit-splash-screen t)

4.4 Initial scratch buffer message

(setq initial-scratch-message "")

4.5 Theme

4.5.1 Hook make-frame to detect if we're in a TTY or GUI

(load-theme 'wheatgrass t)
(enable-theme 'wheatgrass)

4.6 RainbowDelimiters

Highlight matching delimiters with unique colors. It's virtually necessary with emacs-lisp.

(use-package rainbow-delimiters
:commands (rainbow-delimiters-mode)
(add-hook 'prog-mode-hook #'rainbow-delimiters-mode))

4.7 Highlight matching parenthesis

Specifically under the cursor

(defvar show-paren-delay)
(setq show-paren-delay 0.0)
(show-paren-mode t)

4.8 Highlight trailing whitespace

Trailing whitespace is really annoying, especially when pasting from certain terminals.

(defun my/buf-show-trailing-whitespace ()
    (setq show-trailing-whitespace t))
(add-hook 'prog-mode-hook 'my/buf-show-trailing-whitespace)
(custom-set-faces '(trailing-whitespace ((t (:background "dim gray")))))

4.9 Prettify symbols

Convert certain words into symbols. Prime example: lambda becomes λ.


4.10 Show column number in modeline

(column-number-mode t)

4.11 Prettify source code in org-mode

Org mode should have pretty highlighting for source code.

(setq org-src-fontify-natively t)
(setq org-src-tab-acts-natively t)
(setq org-edit-src-content-indentation 0)
(use-package htmlize)

5 General settings

These settings don't belong to any specific mode or package. Some packages are exceptional for being very simple.

5.1 Optimize org-babel config for faster startup

Running org-babel-load-file increases startup time, so only do it when there are changes to update. Only run it when config.el is missing, and delete config.el when config.org changes. (see init.el)

(require 's)
(defun my/delete-config-el ()
    "Delete ~/.emacs.d/config.el when the current buffer is ~/.emacs.d/config.org"
    (defvar my-configel)
    (setq my-configel "~/git/menglehorn-linux-dotfiles/configs/emacs/config.el")
    (if (s-suffix? "emacs/config.org" buffer-file-name)
        (if (file-exists-p my-configel)
            (progn (delete-file "~/git/menglehorn-linux-dotfiles/configs/emacs/config.el")
                   (delete-file "~/git/menglehorn-linux-dotfiles/configs/emacs/config.elc")))))

(add-hook 'after-save-hook 'my/delete-config-el)

5.2 Set up org-babel

We need to allow SH scripts to be executed by org-babel.

(require 'ob-sh)

5.3 Start server if it isn't started

Turn the first Emacs process into a server, which allows reuse of a that process through the shell command emacsclient.

(require 'server)
(if (not (server-running-p)) (server-start))

5.4 Backup & auto-save files in one place

These files are useful if something goes wrong, but they're also annoying in how they clutter directories. Put them in ~/.emacs.d/tmp to remedy this.

(setq backup-directory-alist `((".*" . "~/.emacs.d/tmp"))
        auto-save-file-name-transforms `((".*" , "~/.emacs.d/tmp" t)))

5.5 Scroll smoothly

(setq scroll-margin 0)
(setq scroll-conservatively 10000)
(setq scroll-preserve-screen-position t)

5.6 Sentences end with a single period

(setq sentence-end-double-space nil)

5.7 y/n instead of yes/no

(fset 'yes-or-no-p 'y-or-n-p)

5.8 Wrap text at 80 characters

(setq-default fill-column 80)

5.9 Auto-detect indent settings

I prefer to follow a file's indenting style instead of enforcing my own, if possible. dtrt-indent does this and works for most mainstream languages.

(use-package dtrt-indent)

5.10 Auto-update changed files

If a file is changed outside of Emacs, automatically load those changes.

(global-auto-revert-mode t)

5.11 Auto-executable scripts in *nix

When saving a file that starts with #!, make it executable.

(add-hook 'after-save-hook

5.12 Enable HideShow in programming modes

Useful for getting an overview of the code. It works better in some languages and layouts than others.

(defun my/enable-hideshow ()
    (hs-minor-mode t))
(add-hook 'prog-mode-hook 'my/enable-hideshow)

5.13 Recent Files

Enable recentf-mode and remember a lot of files.

(recentf-mode 1)
(defvar recentf-max-saved-items)
(setq recentf-max-saved-items 200)

5.14 Better same-name buffer distinction

When two buffers are open with the same name, this makes it easier to tell them apart.

(require 'uniquify)
(setq uniquify-buffer-name-style 'forward)

5.15 Remember last position for reopened files

(if (version< emacs-version "25.0")
    (progn (require 'saveplace)
        (setq-default save-place t))
(save-place-mode 1))

5.16 Disable garbage collection in minibuffer

See this article for more info.

(defun my/minibuffer-setup-hook ()
(setq gc-cons-threshold most-positive-fixnum))
(defun my/minibuffer-exit-hook ()
(setq gc-cons-threshold 800000))
(add-hook 'minibuffer-setup-hook #'my/minibuffer-setup-hook)
(add-hook 'minibuffer-exit-hook #'my/minibuffer-exit-hook)

5.17 Configure default web browser

I use Chrome as my default browser.

(defvar browse-url-generic-program)
(if (eq system-type 'darwin)
    (setq browse-url-browser-function 'browse-url-generic
          browse-url-generic-program "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"))
(if (eq system-type 'gnu/linux)
    (setq browse-url-browser-function 'w3m-browse-url))

5.18 Use plink on windows

Windows doesn't have the ssh command.

(when (eq window-system 'w32)
  (setq tramp-default-method "plink"))

6 Install and Set Up packages

6.1 BBDB

Install the Big Brother Database

(use-package bbdb
  (bbdb-initialize 'gnus 'message))
(require 'bbdb)

6.2 request.el

This is an HTTP library for emacs.

(use-package request)

6.3 git

An Elisp API for programmatically using Git.

(use-package git)

6.4 el-get

I use this to grab arbitrary lisp from github.

(add-to-list 'load-path "~/.emacs.d/el-get")
(use-package el-get)
(require 'el-get)

6.5 ERC IRC Client

IRC Client for Emacs

(use-package erc-colorize)
(use-package erc-crypt)
(use-package erc-hl-nicks)
(use-package erc-image)
(use-package erc-social-graph)
(use-package erc-youtube)
(require 'tls)
(make-variable-buffer-local 'erc-fill-column)
(add-hook 'window-configuration-change-hook 
          '(lambda ()
                (lambda (w)
                  (let ((buffer (window-buffer w)))
                    (set-buffer buffer)
                    (when (eq major-mode 'erc-mode)
                      (setq erc-fill-column (- (window-width w) 2)))))))))

6.6 Twitter Mode

Browse Twitter from Emacs

(use-package twittering-mode
  (if (executable-find "convert")
      (setq twittering-convert-fix-size 32))
  (if (executable-find "gzip")
      (setq twittering-use-icon-storage t)))

6.7 Evil Powerline

Powerline for Evil mode

(use-package powerline)
(use-package powerline-evil)

6.8 Magit

Easy Git management

(use-package magit)
(use-package magit-popup)

6.9 Ledger Mode

I use ledger-cli for my personal finances, here I make it evil friendly.

(use-package ledger-mode
  :ensure t
  (setq ledger-clear-whole-transactions 1)

  (add-to-list 'evil-emacs-state-modes 'ledger-report-mode)
  :mode "\\.ldg\\'")

6.10 Smex

Smart M-x for Emacs

(use-package smex
  (global-set-key (kbd "M-x") 'smex))

6.11 Git Commit Mode

Mode for Git Commits

(use-package git-commit)

6.12 JSON Formatter

Command to clean up and prettify json.

(use-package json-mode)

6.13 EMMS

Emacs Multimedia System

(use-package emms)
(use-package emms-info-mediainfo)

(require 'emms-setup)

;; After loaded
;(require 'emms-info-mediainfo)
;(add-to-list 'emms-info-functions 'emms-info-mediainfo)
(require 'emms-info-metaflac)
(add-to-list 'emms-info-functions 'emms-info-metaflac)

(require 'emms-player-simple)
(require 'emms-source-file)
(require 'emms-source-playlist)
(setq emms-source-file-default-directory "~/Music/")

6.14 w3m

Web browser for Emacs

 ((string-equal system-type "windows-nt")
 ((string-equal system-type "gnu/linux")
    (use-package w3m
      :ensure t
      (setenv "PATH" (concat (getenv "PATH") ":/usr/local/bin"))
      (setq exec-path (append exec-path '("/usr/local/bin")))
      (autoload 'w3m-browse-url "w3m")
      (global-set-key "\C-xm" 'browse-url-at-point)
      (setq w3m-use-cookies t)
      (setq w3m-default-display-inline-images t)))))

6.15 multi-term

Multiple terminal manager for Emacs

(use-package multi-term
  (setq multi-term-program "/bin/bash"))
(require 'multi-term)

6.16 web-mode

web-mode.el is an emacs major mode for editing web templates aka HTML files embedding parts (CSS/JavaScript) and blocks (pre rendered by client/server side engines).

(use-package web-mode
    (require 'web-mode)
    (add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.php\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.[agj]sp\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
    (add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode)))

6.17 exec-path-from-shell

This is used to grab envirtonment variables from the shell.

(use-package exec-path-from-shell)
(require 'exec-path-from-shell)

6.18 Company-mode

Company is a text completion framework for Emacs. The name stands for "complete anything". It uses pluggable back-ends and front-ends to retrieve and display completion candidates.

(use-package company
  (add-hook 'after-init-hook 'global-company-mode))

6.19 nnreddit

A Reddit backend for the Gnus newsreader.

;(el-get-bundle nnredit
;  :url "https://github.com/paul-issartel/nnreddit.git"
;  (add-to-list 'gnus-secondary-select-methods '(nnreddit "")))

6.20 Matrix Integration

A Matrix Chat client.

;(use-package matrix-client)
;(require 'matrix-client)
;(setq matrix-homeserver-base-url "https://matrix.productionservers.net")

6.21 govc VMware Integration

A VMware integration script

(use-package govc)

6.22 helm-spotify-plus

I enjoy listening to spotify, bonus points for emacs integration.

(use-package helm-spotify-plus
  (global-set-key (kbd "C-c s s") 'helm-spotify-plus))

6.23 helm-bbdb

Using helm to access and use the BBDB is nice too.

(use-package helm-bbdb
  (global-set-key (kbd "C-c s b") 'helm-bbdb))

6.24 helm-pass

Pass is pretty nice password manager

(use-package helm-pass
  (global-set-key (kbd "C-c s p") 'helm-pass))

6.25 helm-emms

This package makes using emms super easy!

(use-package helm-emms
  (global-set-key (kbd "C-c s e") 'helm-emms))

6.26 vagrant

(use-package vagrant)

6.27 Golang

All the packages associated with golang.

(use-package go-mode)
(use-package go-stacktracer)
(use-package go-complete)
(use-package go-eldoc)

6.28 markdown-mode

Package for editing markdown.

(use-package markdown-mode
  :ensure t
  :commands (markdown-mode gfm-mode)
  :mode (("README\\.md\\'" . gfm-mode)
         ("\\.md\\'" . markdown-mode)
         ("\\.markdown\\'" . markdown-mode))
  :init (setq markdown-command "multimarkdown"))

6.29 calfw

Package for org-mode calendar

(use-package calfw
  (cfw:face-toolbar ((t (:background "midnight blue" :foreground "Steelblue4"))))
  (cfw:face-toolbar-button-off ((t (:foreground "gray30" :weight bold)))))
(use-package calfw-org)
(use-package calfw-ical)
(use-package calfw-cal)

6.30 blockdiag-mode

I need blockdiag-mode for ob-blockdiag

(use-package blockdiag-mode)

7 Org-mode

7.1 Install org-mode

(use-package org)
(require 'org)

7.2 Todo Keywords

Here are my TODO states and color settings.

(setq org-todo-keywords
      (quote ((sequence "TODO(t)" "NEXT(n)" "|" "DONE(d)")
              (sequence "WAITING(W@/!)" "HOLD(h@/!)" "|" "CANCELLED(c@/!)" "PHONE" "MEETING"))))

(setq org-todo-keyword-faces
      (quote (("TODO" :foreground "red" :weight bold)
              ("NEXT" :foreground "blue" :weight bold)
              ("DONE" :foreground "forest green" :weight bold)
              ("WAITING" :foreground "orange" :weight bold)
              ("HOLD" :foreground "magenta" :weight bold)
              ("CANCELLED" :foreground "forest green" :weight bold)
              ("MEETING" :foreground "forest green" :weight bold)
              ("PHONE" :foreground "forest green" :weight bold))))

7.3 Todo Selection

(setq org-use-fast-todo-selection t)

Changing a task state is done with C-c C-t KEY

7.4 Todo State Triggers

Trigger breakdown

  • Moving a task to CANCELLED adds a CANCELLED tag
  • Moving a task to WAITING adds a WAITING tag
  • Moving a task to HOLD adds WAITING and HOLD tags
  • Moving a task to a done state removes WAITING and HOLD tags
  • Moving a task to TODO removes WAITING, CANCELLED, and HOLD tags
  • Moving a task to NEXT removes WAITING, CANCELLED, and HOLD tags
  • Moving a task to DONE removes WAITING, CANCELLED, and HOLD tags
(setq org-todo-state-tags-triggers
      (quote (("CANCELLED" ("CANCELLED" . t))
              ("WAITING" ("WAITING" . t))
              ("HOLD" ("WAITING") ("HOLD" . t))
              (done ("WAITING") ("HOLD"))
              ("TODO" ("WAITING") ("CANCELLED") ("HOLD"))
              ("NEXT" ("WAITING") ("CANCELLED") ("HOLD"))
              ("DONE" ("WAITING") ("CANCELLED") ("HOLD")))))

7.5 Remove empty LOGBOOK drawers

;; Remove empty LOGBOOK drawers on clock out
(defun bh/remove-empty-drawer-on-clock-out ()
    (beginning-of-line 0)
    (org-remove-empty-drawer-at "LOGBOOK" (point))))

(add-hook 'org-clock-out-hook 'bh/remove-empty-drawer-on-clock-out 'append)

7.6 Refile captured

Any file in org-agenda-files are valid refile targets.

; Targets include any file contributing to the agenda - up to 9 levels deep
(setq org-refile-targets (quote ((org-agenda-files :maxlevel . 9))))

; Use full outline paths for refile targets - we file directly with IDO
(setq org-refile-use-outline-path t)

; Targets complete directly with IDO
(setq org-outline-path-complete-in-steps nil)

; Allow refile to create parent tasks with confirmation
(setq org-refile-allow-creating-parent-nodes (quote confirm))

;;;; Refile settings
; Exclude DONE state tasks from refile targets
(defun bh/verify-refile-target ()
  "Exclude todo keywords with a done state from refile targets"
  (not (member (nth 2 (org-heading-components)) org-done-keywords)))

(setq org-refile-target-verify-function 'bh/verify-refile-target)

7.7 Org-bullets

I don't see a reason why you wouldn't want this.

(use-package org-bullets
  :commands (org-bullets-mode)
    (setq org-bullets-bullet-list '("●"))
    (add-hook 'org-mode-hook 'org-bullets-mode))

7.8 Set up notes and todos

Org-mode is great for project management, and I use it quite a bit for that.

(setq org-default-notes-file "~/Nextcloud/org-mode/work.org")

;; Org-Mode Capture Templates
(defvar org-capture-templates)
(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/Nextcloud/org-mode/refile.org" "Tasks")
         "* TODO %?\n%U\n%a\n" :clock-in nil :clock-resume nil)
        ("r" "Respond" entry (file "~/Nextcloud/org-mode/refile.org")
         "* NEXT Respond to %:from %:subject\nSCHEDULED: %t\n%U\n%a\n" :clock-in t :clock-resume t :immediate-finish t)
        ("j" "Journal" entry (file+datetree "~/Nextcloud/org-mode/journal.org")
         "* %? %?\n%U\n" :clock-in t :clock-resume t)
        ("m" "Meeting" entry (file "~/Nextcloud/org-mode/refile.org")
         "* MEETING with %? :MEETING:\n%U" :clock-in t :clock-resume t)
        ("p" "Phone call" entry (file "~/Nextcloud/org-mode/calls.org")
         "* PHONE %? :PHONE:\n%U" :clock-in t :clock-resume t)))

(global-set-key (kbd "C-c c") 'org-capture)
(global-set-key (kbd "C-c a") 'org-agenda)
(global-set-key (kbd "C-c l") 'org-store-link)
(setq org-log-done 'time)

7.9 ox-twbs

Twitter Bootstrap or just Bootstrap is prettier than out of the box Org-mode. Let's get it installed!

(use-package ox-twbs)

7.10 ox-latex

ox-latex allows syntax highlighted output. Just make sure to run the following code.

sudo pip install pygments

And set up ox-latex

(require 'ox-latex)
(add-to-list 'org-latex-packages-alist '("" "minted"))
(setq org-latex-listings 'minted)
(setq org-latex-pdf-process
      '("xelatex -shell-escape -interaction nonstopmode -output-directory %o %f"))

7.11 ox-mediawiki

ox-mediawiki allows exporting org-mode documents to Mediawiki

(use-package ox-mediawiki
  (require 'ox-mediawiki))

7.12 ob-dot

Let's set up capacity to create pretty diagrams

(require 'ob-dot)

7.13 ob-ledger

Org Babel Ledger Blocks!

(require 'ob-ledger)

7.14 ob-sql

ob-sql allows me to add MySQL queries to org-mode and execute them.

(use-package sql
  (require 'sql))
(require 'ob-sql)

7.15 ob-blockdiag

Create diagrams in org-mode.

(use-package ob-blockdiag
  (require 'ob-blockdiag))

7.16 ob-ledger

Call ledger from org-mode

(require 'ob-ledger)

7.17 org-trello

Gotta connect to Trello!

(use-package org-trello)
(require 'org-trello)
(add-to-list 'auto-mode-alist '("\\.trello$" . org-mode))
(add-hook 'org-mode-hook
          (lambda ()
            (let ((filename (buffer-file-name (current-buffer))))
              (when (and filename (string= "trello" (file-name-extension filename)))
(defun my-trello-mode-setup ()
  (setq browse-url-browser-function 'browse-url-firefox))
(add-hook 'org-trello-mode-hook 'my-trello-mode-setup)

7.18 org-publish

I have a website to publish!

(setq org-publish-project-alist
         :base-directory "~/Nextcloud/org/"
         :base-extension "org"
         :publishing-directory "/root@websites01.lan.productionservers.net:/mnt/sites/michael.englehorn.com/public_html/"
         :recursive t
         :publishing-function org-twbs-publish-to-html
         :headline-levels 4
         :auto-preamble t)
         :base-directory "~/Nextcloud/org/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|php\\|txt\\|asc"
         :publishing-directory "/root@websites01.lan.productionservers.net:/mnt/sites/michael.englehorn.com/public_html/"
         :recursive t
         :publishing-function org-publish-attachment)
         :base-directory "~/Nextcloud/org-mode/work/"
         :base-extension "org"
         :publishing-directory "/menglehorn@apollo:~/public_html/"
         :recursive t
         :with-timestamps t
         :publishing-function org-twbs-publish-to-html
         :headline-levels 4
         :auto-preamble t)
         :base-directory "~/Nextcloud/org-mode/work/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|php\\|txt\\|asc"
         :publishing-directory "/menglehorn@apollo:~/public_html/"
         :recursive t
         :publishing-function org-publish-attachment)
        ("org" :components ("org-notes" "org-static"))))

(require 's)
(defun my/publish-work ()
  "Publish ~/Nextcloud/org-mode/work/ whenever work.org is saved."
  (if (equal "~/Nextcloud/org-mode/work/work.org" buffer-file-truename)
      (progn (setq org-confirm-babel-evaluate nil)
             (setq org-export-babel-evaluate t)
             (org-publish "org-work")
             (org-publish "org-work-static")
             (setq org-confirm-babel-evaluate t))))

(add-hook 'org-mode-hook
          (lambda ()
            (add-hook 'after-save-hook 'my/publish-work nil nil)))

8 Customizations

8.1 Load SSH Agent from environment

(when (not (eq window-system 'w32))
    (exec-path-from-shell-copy-env "SSH_AGENT_PID")
    (exec-path-from-shell-copy-env "PATH")
    (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))

8.2 Powerline

Initialize the Powerline.

(require 'powerline)

8.3 Powerline theme

Set up the powerline theme


8.4 Load secrets

I keep slightly more sensitive information in a separate file so that I can easily publish my main configuration.

(el-get-bundle menglehorn-emacs-secret
  :url "git@github.com:K0HAX/menglehorn-emacs-secret.git"
  :feature "menglehorn-emacs-secret"
  :submodule nil
  (load-file "~/.emacs.d/el-get/menglehorn-emacs-secret/menglehorn-emacs-secret.elc"))

8.5 Restart Emacs

It's useful to be able to restart emacs from inside emacs. Configure restart-emacs to allow this.

(use-package restart-emacs)

8.6 Ledger Reports

Some custom and shorter keywords for ledger reports (C-r r in ledger-mode)

(setq ledger-reports
       (("register" "ledger ")
        ("bal" "ledger -f %(ledger-file) bal Assets Liabilities")
        ("bigbal" "ledger -f %(ledger-file) bal")
        ("reg" "ledger -f %(ledger-file) reg")
        ("payee" "ledger -f %(ledger-file) reg @%(payee)")
        ("account" "ledger -f %(ledger-file) reg %(account)"))))

8.7 Newsticker List

Set up RSS feeds

(setq newsticker-url-list-defaults
    (("NY Times" "http://partners.userland.com/nytRss/nytHomepage.xml")
    ("The Register" "http://www.theregister.co.uk/tonys/slashdot.rdf")
    ("slashdot" "http://slashdot.org/index.rss" nil 3600))))

8.8 Mail Processing

Set up the mail sending function

(setq send-mail-function 'smtpmail-send-it)

8.9 Twittering Mode Settings

We want to encrypt the Oauth credentials for twittering-mode

(setq twittering-use-master-password t)
(require 'twittering-mode)
(defun twit ()
  (let ((browse-url-browser-function 'browse-url-firefox))

8.10 Customizations file

Move the customizations file out from init.el so we don't break the Git repo.

(setq custom-file "~/.emacs.d/emacs-custom.el")
(load custom-file)

8.11 Disable blinking and flashing

Disable the annoying bell

(setq ring-bell-function 'ignore)

8.12 M-s s to SSH to a host.

I wanted to by able to easily SSH from Emacs, so I wrote some elisp.

(defun ssh-to-host (x)
  "Ask for host."
  (interactive "sHost: ")
  (let* ((buffer-name (format "*SSH %s*" x))
         (buffer (get-buffer buffer-name)))
    (if buffer
        (switch-to-buffer buffer)
       (get-buffer-process (rename-buffer buffer-name))
       (format "ssh %s\r" x)))))

(global-set-key (kbd "M-s s") 'ssh-to-host)

8.13 Ask to open as root if I lack permission to edit

Very useful. If I try to open a file I don't have write permissions to, ask if I want to open it as root using tramp.

Note: if you're experiencing problems using this (like tramp hanging), check that you can open them "manually" in the first place, C-x C-f /sudo::/path/to/file. Check the tramp troubleshooting section at emacs wiki.

(defun th-rename-tramp-buffer ()
  (when (file-remote-p (buffer-file-name))
     (format "%s:%s"
             (file-remote-p (buffer-file-name) 'method)

(add-hook 'find-file-hook

(defadvice find-file (around th-find-file activate)
  "Open FILENAME using tramp's sudo method if it's read-only."
  (if (and (not (file-writable-p (ad-get-arg 0)))
           (not (file-remote-p default-directory))
           (y-or-n-p (concat "File "
                             (ad-get-arg 0)
                             " is read-only.  Open it as root? ")))
      (th-find-file-sudo (ad-get-arg 0))

(defun th-find-file-sudo (file)
  "Opens FILE with root privileges."
  (interactive "F")
  (set-buffer (find-file (concat "/sudo::" file))))

8.14 Dired customizations

  • Human readable sizes in Dired
  • Sort by size
(setq dired-listing-switches "-alh")

8.15 Key bindings

Set up custom global key bindings.

(global-set-key (kbd "<f12>") 'org-agenda)
(global-set-key (kbd "<f8>") 'org-cycle-agenda-files)
(global-set-key (kbd "<f9> b") 'bbdb)
(global-set-key (kbd "<f9> c") 'my-open-calendar)
(global-set-key (kbd "<f9> g") 'gnus)
(global-set-key (kbd "<f11>") 'org-clock-goto)
(global-set-key (kbd "C-c c") 'org-capture)


9.1 Automatically add people to the bbdb

(bbdb-initialize 'gnus 'message)
(bbdb-mua-auto-update-init 'message)
(setq bbdb-mua-auto-update-p 'query)

10 Mutt

10.1 Emacs as external editor

(add-to-list 'auto-mode-alist '("/mutt" . mail-mode))

10.2 Autofill for Mail

(add-hook 'mail-mode-hook 'turn-on-auto-fill)

10.3 Replace C-x # with C-x k

(defun my-mail-mode-hook ()
  (auto-fill-mode 1)
  (abbrev-mode 1)
  (local-set-key "\C-Xk" 'server-edit))
(add-hook 'mail-mode-hook 'my-mail-mode-hook)

11 Evil Mode

11.1 Enable evil mode

(use-package evil)
(use-package evil-leader)
(use-package evil-org)
(require 'evil)
(evil-mode 1)

11.2 Set up global key-bindings, and make evil my leader.

(eval-after-load "evil"
    (define-key evil-normal-state-map (kbd "M-h") 'evil-window-left)
    (define-key evil-normal-state-map (kbd "M-j") 'evil-window-down)
    (define-key evil-normal-state-map (kbd "M-k") 'evil-window-up)
    (define-key evil-normal-state-map (kbd "M-l") 'evil-window-right)
    (define-key evil-motion-state-map ";" 'smex)
    (define-key evil-motion-state-map ":" 'evil-ex)))

11.3 Set up key-bindings for ledger-mode

(with-eval-after-load 'ledger-mode
  (define-key ledger-mode-map (kbd "C-c n") 'ledger-add-transaction)
  (define-key ledger-mode-map (kbd "C-c c") 'ledger-mode-clean-buffer)
  (define-key ledger-mode-map (kbd "C-c r") 'ledger-report))

11.4 Set up key-bindings for BBDB

Set up the Key Bindings for the Big Brother Database. This code was pulled from github/tarleb

(evil-define-key 'motion bbdb-mode-map
  "\C-k"       'bbdb-delete-field-or-record
  "\C-x\C-s"   'bbdb-save
  "\C-x\C-t"   'bbdb-transpose-fields
  "\d"         'bbdb-prev-field ; DEL
  "\M-d"       'bbdb-dial
  "\t"         'bbdb-next-field ; TAB
  "+"          'bbdb-append-display
  "*"          'bbdb-do-all-records
  ";"          'bbdb-edit-foo
  "?"          'bbdb-help
  "!"          'bbdb-search-invert
  "="          'delete-other-windows
  "a"          'bbdb-add-mail-alias
  "A"          'bbdb-mail-aliases
  "C"          'bbdb-copy-records-as-kill
  "c"          'bbdb-create
  "d"          'bbdb-delete-field-or-record
  "e"          'bbdb-edit-field
  "h"          'bbdb-info
  "i"          'bbdb-insert-field
  "J"          'bbdb-next-field
  "j"          'bbdb-next-record
  "K"          'bbdb-prev-field
  "k"          'bbdb-prev-record
  "m"          'bbdb-mail
  "M"          'bbdb-mail-address
  "N"          'bbdb-next-field
  "n"          'bbdb-next-record
  "o"          'bbdb-omit-record
  "P"          'bbdb-prev-field
  "p"          'bbdb-prev-record
  "s"          'bbdb-save
  "T"          'bbdb-display-records-completely
  "t"          'bbdb-toggle-records-layout
  "u"          'bbdb-browse-url

  ;; Search keys
  "b"          'bbdb
  "/1"         'bbdb-display-records
  "/n"         'bbdb-search-name
  "/o"         'bbdb-search-organization
  "/p"         'bbdb-search-phone
  "/a"         'bbdb-search-address
  "/m"         'bbdb-search-mail
  "/N"         'bbdb-search-xfields
  "/x"         'bbdb-search-xfields
  "/c"         'bbdb-search-changed
  "/d"         'bbdb-search-duplicates
  "\C-xnw"     'bbdb-display-all-records
  "\C-xnd"     'bbdb-display-current-record

(evil-set-initial-state 'bbdb-mode 'motion)

11.5 Fix term-mode

Term-mode shouldn't have evil bindings.

(evil-set-initial-state 'term-mode 'emacs)

11.6 Fix govc-mode

Govc shouldn't have evil bindings.

(evil-set-initial-state 'govc-mode 'emacs)
(evil-set-initial-state 'govc-mode-major-mode 'emacs)

11.7 Fix twittering-mode

Twittering should have some evil bindings.

(eval-after-load 'twittering-mode
     (evil-define-key 'motion twittering-mode-map
                                  "g" nil
                                  "g g" 'twittering-goto-first-status
                                  "c" 'twittering-current-timeline
                                  "C-u" 'twittering-scroll-down
                                  "/" 'evil-search-forward
                                  "?" 'evil-search-backward
                                  "n" 'evil-search-next
                                  "N" 'evil-search-previous
                                  [escape] 'twittering-edit-cancel-status
                                  "<tab>" 'twittering-goto-next-uri)
     (evil-set-initial-state 'twittering-mode 'motion)))

12 Disabled Configs

This section is where bad sections of this config can go if they break Emacs. It's only used for debugging purposes.

12.1 homeassistant-mode

A bit of elisp to allow me to control my home lighting from inside emacs.

(use-package request)
(when (not (eq window-system 'w32))
  (el-get-bundle homeassistant-mode
    :url "https://github.com/K0HAX/homeassistant-mode.git"
    :submodule nil
    (load-file "~/.emacs.d/el-get/homeassistant-mode/homeassistant-mode.elc")
    (setq mje/homeassistant-url "http://home-assistant.lan.productionservers.net:8123")
    (global-set-key (kbd "C-c s h") 'helm-homeassistant)))