Using a copy of iTerm 2 as Emacs.app

I now use the GNU Emacs.app, but when I started using Emacs, I made my own Emacs.app by making a copy of iTerm 2:

cp -R /Applications/iTerm.app /Applications/Emacs.app
defaults write /Applications/Emacs.app/Contents/Info.plist CFBundleIdentifier my.emacs

Changing the bundle identifier makes the application use a different preferences file than iTerm 2 and allows it to be open at the same time as iTerm 2.

I changed the command that is ran in new shells to /usr/local/bin/emacs from the preferences.

I used Platypus to create an application that runs emacsclient -n "$@";open -b my.emacs and made it the default application for text and code files. I added

(require 'server)
(or (server-running-p) (server-start))

to ~/.emacs.

The two main reasons why I started using the GNU Emacs.app instead of the copy of iTerm 2 are that in a text terminal many key combinations cannot be used in keybindings, such as key combinations that include a super or hyper key, and that I did not figure out how to share the kill ring with the OS X clipboard like Emacs.app does by default.

Copy and paste for text terminals

(defun pbcopy ()
  (let ((deactivate-mark t))
    (call-process-region (point) (mark) "pbcopy")))

(defun pbpaste ()
  (call-process-region (point) (if mark-active (mark) (point)) "pbpaste" t t))

(defun pbcut ()
  (delete-region (region-beginning) (region-end)))

(global-set-key (kbd "C-c c") 'pbcopy)
(global-set-key (kbd "C-c v") 'pbpaste)
(global-set-key (kbd "C-c x") 'pbcut)

Using Karabiner to add a hyper key and to add an OS X-wide shortcut for ido-find-file

I use the right control key as a hyper key:

(setq ns-right-control-modifier 'hyper)

I have used Karabiner to map the home key, which is under a thumb on my keyboard, to the right control key in Emacs, and to the combination of control, option, shift, and command in other applications:

<?xml version="1.0"?>
    <autogen>__KeyToKey__ KeyCode::HOME, KeyCode::CONTROL_R</autogen>
    <autogen>__KeyToKey__ KeyCode::HOME, ModifierFlag::NONE, KeyCode::CONTROL_L, ModifierFlag::CONTROL_L | ModifierFlag::OPTION_L | ModifierFlag::SHIFT_L | ModifierFlag::COMMAND_L</autogen>
    <autogen>__KeyToKey__ KeyCode::F, VK_CONTROL | VK_OPTION | VK_SHIFT | VK_COMMAND, KeyCode::VK_OPEN_URL_EMACS, KeyCode::VK_WAIT_100MS, KeyCode::F, ModifierFlag::CONTROL_R</autogen>

H-f opens a file using the home directory as the default location:

(global-set-key (kbd "H-f") (lambda () (interactive) (ido-find-file-in-dir "~/")))

The last autogen line above makes pressing the f key while holding my custom hyper key also switch to Emacs when another application is frontmost.

Full screen

Use a custom implementation for full screen windows so that there is no animation for switching between Emacs and other applications:

(setq-default ns-use-native-fullscreen nil)

Enter full screen when Emacs is opened:

(setq initial-frame-alist '((fullscreen . fullboth)))


Save buffers when switching from Emacs to another application:

(add-hook 'focus-out-hook (lambda () (let ((inhibit-message t)) (save-some-buffers t))))

focus-out-hook was added in Emacs 24.4 and inhibit-message was added in Emacs 25. Setting inhibit-message to t disables the message about what files are saved, which would otherwise even be shown over the minibuffer when the minibuffer is active.

Save buffers when switching to another buffer or window:

(defadvice switch-to-buffer (before save-buffer-now activate)
  (when buffer-file-name (let ((inhibit-message t)) (save-some-buffers t))))

(defadvice other-window (before other-window-now activate)
  (when buffer-file-name (let ((inhibit-message t)) (save-some-buffers t))))

Revert a buffer associated with a file without asking when the file changes on disk:

(global-auto-revert-mode 1)

Make s-w save and kill a buffer:

(defun save-and-kill-this-buffer ()
  (if (and (buffer-modified-p) (not buffer-read-only) (buffer-file-name)) (save-buffer))

(global-set-key (kbd "s-w") 'save-and-kill-this-buffer)

Setting a default PATH for Emacs and other applications

Save a property list like this as ~/Library/LaunchAgents/my.startup.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <string>launchctl setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>

The property list gets loaded the next time you log in, so you do not have to load it manually.

The lines

<string>launchctl setenv PATH /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>

can be replaced with


but this method allows you to add other commands to the file later.

This does not change the path in applications that are opened as login items or that are reopened at login after a forced shutdown. If you have Emacs, a terminal application, or other applications where you want to change the path in login items, you will have to remove them. After a forced shutdown, you either have to choose the option to not restore open applications, or you have to quit and reopen applications that get restored in order to change the path in them.


brew install emacs --cocoa installs Emacs including the GNU Emacs.app. brew update;brew upgrade emacs updates Homebrew and upgrades Emacs.

(setq ns-antialias-text nil) disables anti-aliasing.

(set-fontset-font "fontset-default" 'japanese-jisx0208 '("Hiragino Kaku Gothic ProN" . "iso10646-1")) uses a Japanese instead of a Chinese font for displaying Japanese and Chinese characters.

(setq shell-file-name "/usr/local/bin/bash") makes shell-command and shell-command-on-region use a version of Bash installed with Homebrew, and (setq shell-command-switch "-lc") makes them invoke Bash as a login shell that reads ~/.bash_profile.

(setq ring-bell-function 'ignore) disables the audible bell. When the alert volume is set to zero from System Preferences, the audible bell is not played, but it causes the sound output to be activated in a way that a quiet hiss is produced for some period of time.