So, I started a new job. Besides actually leaving my home to at least occasionally go into the office, the other big change is I’ve been given a brand-spanking-new MacBook Pro to use (our shop is a part of the Apple Consultant Network).

While using Linux professionally for the last 13 years is sort of coming to an end, Linux certainly isn’t going away from my life, especially after using it almost exclusively at home for 20 years.

The MacBook is still a BSD unix system at heart – with some great hardware, and a lovely, albeit sometimes frustrating, user interface laid on top. Mac enthusiasts might abhor that the only application I set to launch on boot is the terminal. This article is not for them, because in old-school fashion I’m going to cover installing and using Emacs and some other extras in OSX.

Installing Emacs

Installing Emacs via homebrew is very easy so I won’t go into it, you can follow these instructions: http://wikemacs.org/wiki/Installing_Emacs_on_OS_X#Homebrew

OSX 10.11 (El Capitan) actually includes GNU Emacs 22, but it’s only the terminal based version. If you want the newest version and a full GUI, follow the homebrew instructions above. If you need to move or rename the old version, you may need to keep OSX’s attributes and latest security layers in mind. Rather than moving my old version, I added an alias to my ~/.bashrc:

alias emacs="/usr/local/Cellar/emacs/24.5/Emacs.app/Contents/MacOS/Emacs"

However, OSX’s bash won’t read and run~/.bashrc, only ~/.bash_profile, so to my ~/.bash_profile I added:

[[ -s ~/.bashrc ]] && source ~/.bashrc

In my ~/.bash_profile I also added a ~/bin directory to my path where I can put custom scripts:

export PATH="~/bin:$PATH"

To shorten up emacs I added a shell script called em to my ~/bin directory:

#!/bin/sh
open -n -a /Applications/Emacs.app "$@" &

One caution about doing something similar to this is that em (edit) is now very close to rm (remove) since the ‘E’ and ‘R’ keys are neighbors. Just make sure you know what you’ve typed before hitting return 🙂

Fix Home/End Keys

I’m not sure why the Home/End keys don’t have normal behavior as I’ve become accustomed to. Here are some fixes I’ve found that work:

Home/End for most applications

$ mkdir -p ~/Library/KeyBindings/
$ vi ~/Library/KeyBindings/DefaultKeyBinding.dict

Then add the following:

{
    "\UF729"  = moveToBeginningOfParagraph:; // home
    "\UF72B"  = moveToEndOfParagraph:; // end
    "$\UF729" = moveToBeginningOfParagraphAndModifySelection:; // shift-home
    "$\UF72B" = moveToEndOfParagraphAndModifySelection:; // shift-end
}

Home/End in Terminal

Open Terminal, then go to Terminal -> Preferences…

Go to the Profiles section, highlight the terminal profile you use the most, then click the “Keyboard” tab. You’ll probably have to add two new entries for Home and End, but first highlight F1, click edit, then copy the \033OP action code so you can paste it in the new codes. Click cancel after copying the F1 code.

Click ‘+’ to add Home. Choose Key: “Home”, Modifier: “None”, Action: “Send Text:”, and paste in the action code. Click “Delete One Character” and then type “H”. You should end up with \033OH for Home. Follow the same procedure for End, but map it to \033OF.

Home/End In Emacs

Edit your ~/.emacs file (you could instead edit it with Emacs and it would be oh so meta).

$ vi ~/.emacs

Add the following:

; Fix home/end in OSX
(define-key global-map [home] 'beginning-of-line)
(define-key global-map [end] 'end-of-line)

PHP Add-ons

I wanted to add a PHP Code Sniffer to do syntax checking and documentation/standards enforcement. To add it via homebrew, I first needed to add the alternate homebrew-php repository. See the installation instructions here. Then I could install phpcs:

brew install php-code-sniffer

I added this because I want to use the official PHP Code Sniffer for WordPress.

I installed the Code Sniffer for WordPress from github into my ~/Sites directory and then added it to my phpcs configuration by running:

phpcs --config-set installed_paths /Users/jfoell/Sites/WordPress-Coding-Standards

Then I verified that it was installed by running:

phpcs -i

The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP

PHP Code Sniffer in Emacs via MELPA and Flycheck

I added MELPA stable packages to my ~/.emacs file (see their installation instructions), then updated MELPA from within Emacs:

M-x package-list-packages RET

Then I installed the flycheck extension (which will use PHP CodeSniffer to do on-the-fly syntax checking):

M-x package-install RET flycheck RET

Then I added the following default phpcs standard setting and init hook to my ~/.emacs file:

(custom-set-variables
 '(flycheck-phpcs-standard "WordPress")
)

(add-hook 'after-init-hook #'global-flycheck-mode)

Other Emacs and Terminal Goodies

To add Tab Bar Mode support, I cloned the Aquamacs tabbar project into ~/.elisp/tabbar. Then I added these lines to my ~/.emacs file to load the tabbar library and start tab bar mode automatically:

;; Load custom tabbar mode
(load-file "~/.elisp/tabbar/tabbar.el")
(tabbar-mode)

Lastly, here’s a great article on getting the terminal prompt and output to look a little nicer: http://osxdaily.com/2013/02/05/improve-terminal-appearance-mac-os-x/

Happy Hacking!

One thought on “Emacs on OSX

  1. Pingback: Windows-X/C/V Cut/Copy/Paste in Ubuntu - Justin Foell

Leave a Reply