Sep 28

Emacs stuff

emacs setup

Yep, I think almost every developer out there has a try Emacs todo on their list. All others seems to already use it for decades. In the ruby community people like defunkt caused a little renaissance, but I might be wrong and this is my very own impression.

Anyway, I've installed Emacs several times (even before GitHub & Co.). But honestly I still can't get used to it. Nevertheless its a tool in your box, and you should know how to close it at least. Just kidding! :)

Try it yourself. Maybe you'll find something which makes you to switch to Emacs. On this page I'm collecting all stuff to learn and pimp my setup. Enjoy!

Cheat sheet for shortcuts

The problem you have to take care of when learing Emacs is to learn its keys/shortcuts. As with for example vim its real power is a quick navigation of all functions under your fingertips. So the first weeks you definitely need a XEmacs Reference Card. This one may be a little outdated, but very instructable and handy if printed out.

Some useful sites with instructions, tips and tricks. I've found most of the stuff below on these sites.

Add a dark color theme

Just because I like the colors of the Tango Desktop Project and a dark background color. The Emacs Wiki gives more detailed informations about Color themes in general.

But this is how I installed this sweet color-theme-subdued by Jason Blevins I came across.

  • Create a ~/.emacs.d/vendor directory

  • Open or create the ~/.emacs file and add the dir to load_path

        (add-to-list 'load-path "~/.emacs.d/vendor")
  • Download the color-theme-subdued file

  • Move the file in the just created ~/.emacs.d/vendor

  • Load the scheme in your ~/.emacs file

        ;; default color scheme
        (require 'color-theme-subdued)

Map Caps Lock to Control

In Emacs you have to use CTRL for nearly every command. So moving your hand to much is very uncomfortable. The solution is to map the (almost) unused CAPS-LOCK as a second CTRL key. This is even the no. 1 tip of Steve Yegge's great post 10 Specific Ways to Improve Your Productivity With Emacs.

After using it a while I even remapped CAPS-LOCK not for Emacs only, but for the whole OS. Here is an easy description how you can Map CAPS-LOCK to Control in Ubuntu and Mac OS X. Without all these neat screenshots in Ubuntu it's quite simple:

  • Open the dialog System » Preferences » Keyboard » Layout Options…
  • Set the radio button Ctrl key position » Make CapsLock an additional Ctrl
  • Done!

Highlight the current line

For more options please read Highlight the Current Line post of the M-x all-things-emacs blog.

But this is what I put in my ~/.emacs file:

(global-hl-line-mode 1)
(set-face-background 'hl-line "#330")

Reload .emacs without restarting Emacs

Ok, almost everything is configured in your ~/.emacs file. And as a beginner it's quite annoying to edit, save, close, open and find the file again. But there's a way on How to reload your .emacs file while emacs is running

  • M-x load-file ENTER
  • ~/.emacs ENTER

More advanced users may add a shortcut for this. It's up to you to find out how. :)

Use a line instead a block cursor

Block cursors as old-school as CRT displays. Further all commands use the beginning of the block cursor (for example delete). Which confused me a little, because I was used to the line cursors.

But of course you can change everything in Emacs! Again, the Emacs Wiki gives instructions how to Change the Cursor Dynamically

  • Add this cursor-chg.el to your load-path.

  • Add this to your ~/.emacs file to require it.

        (require 'cursor-chg)
        (toggle-cursor-type-when-idle -1)
        (change-cursor-mode 1)

Add Markdown support

Jason Blevins created a great Emacs markdown-mode. A must have these days!

  • Clone the repository and put the files in your load-path:

        git clone git://
  • Set markdown-mode manually for the current file:

        M-x markdown-mode
  • Register the mode for md, mkdn and markdown files:

        ;; markdown-mode
        (setq auto-mode-alist
           (cons '("\\.md" . markdown-mode) auto-mode-alist))
        (setq auto-mode-alist
           (cons '("\\.mkdn" . markdown-mode) auto-mode-alist))
        (setq auto-mode-alist
           (cons '("\\.markdown" . markdown-mode) auto-mode-alist))

Move lines up and down

I got used to moveing lines in Eclipse and Gedit. Actually I can't handle my TODO lists without these shortcuts anymore! This is the Move Line script by Joe Smith. Basically it maps some global keys to the move-line function. Nice!

(defun move-line (n)
  "Move the current line up or down by N lines."
  (interactive "p")
  (setq col (current-column))
  (beginning-of-line) (setq start (point))
  (end-of-line) (forward-char) (setq end (point))
  (let ((line-text (delete-and-extract-region start end)))
    (forward-line n)
    (insert line-text)
    ;; restore point to original column in moved line
    (forward-line -1)
    (forward-char col)))

(defun move-line-up (n)
  "Move the current line up by N lines."
  (interactive "p")
  (move-line (if (null n) -1 (- n))))

(defun move-line-down (n)
  "Move the current line down by N lines."
  (interactive "p")
  (move-line (if (null n) 1 n)))

(global-set-key (kbd "M-<up>") 'move-line-up)
(global-set-key (kbd "M-<down>") 'move-line-down)
✱ ✱ ✱