Javier Tordable

I write software at Google. I also enjoy Mathematics, Technology and Design.

Two emacs tips

November 04, 2012

I've been using emacs for a few years now. And even though I am not an expert I appreciate how powerful it is. It never ceases to amaze me how many configuration options are available.

The first tip I want to share is about formatting rules. For example, I write my code in lines of at most 80 characters (and you all should do too, see the style guide). It's nice to have an indication from the editor if the line is longer. So I set up emacs to turn the font weight to bold when the line is too long. I also have a special configuration to indicate tabs (which are forbidden) and trailing whitespaces at the end of the line (which are forbidden as well).

;; Special fonts for > 80 chars, tabs and trailing whitespace.
(custom-set-faces
  '(my-long-line-face ((((class color)) (:weight bold))) t)
  '(my-trailing-space-face ((((class color)) (:background "PeachPuff"))) t))

;; Mark lines longer than 80 characters, and indicate trailing whitespace.
(add-hook 'font-lock-mode-hook
   (function (lambda ()
     (setq font-lock-keywords
       (append font-lock-keywords
         '(("^.\\{81,\\}$" (0 'my-long-line-face t))
           ("[ \t]+$" (0 'my-trailing-space-face t))))))))

The second tip is to enable emacs to highligt links, to be able to click on them from within emacs and open the link in Chrome. Or if you want to use the keyoard only, move to the link and press C-c <RET>.

;; Add Chrome to the $PATH.
(add-to-list 'exec-path "/opt/google/chrome")

;; Set it as default.
(setq browse-url-browser-function 'browse-url-generic
      browse-url-generic-program "chrome")

;; After loading the file, follow links.
(add-hook 'find-file-hook 'goto-address-mode)

And with these changes emacs looks like this:

emacs configuration options