04 September 2014

Emacs Setup for Version 24.3

A few years ago now, I created a guide to getting started with Emacs for Rails development, and one of the key things I setup was ECB - The Emacs Code Browser.

It turns out things have moved on since the first time I did this (Emacs 22) and even my updated instructions for Emacs 23. I recently did a clean setup on Emacs 24.3 on Centos 7, and found things are much easier now - mostly because of Melpa and Marmalade, which make installing packages almost as simple as installing a Ruby Gem!

So, this is my guide to getting Emacs 24.3 working for my development, starting with ECB and then a few others things.

A few Essential Settings

These are some settings I have picked up over the years that I stick at the top of my .emacs:

;;; Allows syntax highlighting to work, I think
(global-font-lock-mode 1)
;;; Prevents the startup screen displaying
(setq inhibit-splash-screen t) 
;;; Prompts when you attempt to quit (C-x C-c) incase you do it by accident
(setq confirm-kill-emacs 'yes-or-no-p) 
;;; Turns off the tool and menu bars
(tool-bar-mode -1)
(menu-bar-mode -1)
;;; Don't use tabs when indenting code
(setq indent-tabs-mode nil)
;;; When you hit Return, also indent the line (savings having to type tab too)
(define-key global-map (kbd "RET") 'newline-and-indent)

Configure Package Manager

First, add both the Melpa and Marmalade repos to your .emacs:

(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/"))
(add-to-list 'package-archives
             '("melpa" . "http://melpa.milkbox.net/packages/") t)

Now, you can list all the packages availabled with M-x package-list-packages command.

Install ECB

From the list of packages above you can scroll around and find ECB. Luckily, its dependency CEDET is already installed by default in Emacs 24.3, so ECB installation is a simple matter of installing its package! If you select it from the list of packages, then select the install button, it should download and install.

In my .emacs, I only need to add the following code block that sets it up the way I like it:

 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(ecb-layout-name "left14")
 '(ecb-layout-window-sizes nil)
  ;; Adjust the layout 22% of width is directories and 78% is code window. Shrink the history window down to only 5% of height
 '(ecb-layout-window-sizes (quote (("left14" (ecb-directories-buffer-name 0.22631578947368424 . 0.7821428571428571) (ecb-history-buffer-name 0.22631578947368424 . 0.05)))))
 '(ecb-primary-secondary-mouse-buttons (quote mouse-1--C-mouse-1))
 '(ecb-options-version "2.40")
 '(ecb-tip-of-the-day nil)
 '(ecb-tree-buffer-style (quote ascii-guides))

(setq ecb-source-path '("~/source"))

Notice the ecb-source-path - you can add many different source paths here, depending on where your code is stored etc.

Enhanced Ruby Mode

Back when I first started with Ruby, emacs did not come with a builtin Ruby mode - fortunately, now it does. However there is a better one called enh-ruby-mode - Enhanced Ruby Mode. Again, install it from the package manager, and then add the following to .emacs to configure it to open whatever types of Ruby files you want:

(add-to-list 'auto-mode-alist '("\\.rb$" . enh-ruby-mode))
(add-to-list 'auto-mode-alist '("\\.rake$" . enh-ruby-mode))
(add-to-list 'auto-mode-alist '("Gemfile$" . enh-ruby-mode))
(eval-after-load "enh-ruby-mode"
 ;; Use wavy underlines for syntax errors and warnings, not a box.
   '(erm-syn-warnline ((t (:underline (:style wave :color "orange")))))
   '(erm-syn-errline ((t (:underline (:style wave :color "red")))))))

Enhanced Ruby Mode also does live syntax checking as you type, which is pretty handy.


There is a builtin Javascript mode, but again there is a better one called js2-mode - install it using the package manager. The advantage of js2-mode is that is does syntax checking as you type, and I previously used it as my major Javascript mode, however based on the recommendations, it is better to let it work as a minor mode. The default Javascript mode indent is 4, so I changed it to 2:

(add-hook 'js-mode-hook 'js2-minor-mode)
(setq js-indent-level 2)

Color Scheme

I like the Tango color theme, so I install it with the package manger (both color-theme and color-theme-tango), and add the following to my .emacs. For some reason I had to actually include these files, unlike with all the other packages I installed:

;; Color Theme
(add-to-list 'load-path "~/.emacs.d/elpa/color-theme-20080305.34/")
(load-file "~/.emacs.d/elpa/color-theme-20080305.34/color-theme.el")

(add-to-list 'load-path "~/.emacs.d/elpa/color-theme-tango-0.0.2/")
(load-file "~/.emacs.d/elpa/color-theme-tango-0.0.2/color-theme-tango.el")
(require 'color-theme)

Markdown Mode

Install markdown-mode from the package manager and then add

(add-to-list 'auto-mode-alist '("\\.text\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
blog comments powered by Disqus