Version 21 (modified by jstolarek, 4 years ago) (diff)


Emacs tips and tricks

Most of us use Emacs for GHC development. This page lists various Emacs configuration tips and tricks used by GHC developers. It is meant to be self-contained so that you can grab relevant piece of configuration, put it into your config file and benefit from it instantly. The only exception are two larger extensions for Emacs (haskell-mode and ghc-mod), where we direct you to external installation instructions and tutorials.

Each entry includes a short description and instruction how to enable given setting (if it contains only a block of lisp code it means you need to add it to your .emacs configuration file). All configuration is given for Emacs 24, unless otherwise noted.

It is assumed that you know how to install packages in Emacs (e.g. with ELPA). TODO Let's not assume it. Can we create a section "Installing extensions with ELPA" and give a short introduction on using it? I admit I don't remember how to use ELPA and would have to google it - JS.


IDO mode

Description: IDO stands for Interactively Do Things and it greatly improves file opening and switching between buffers. When opening a file it shows list of files and directories in current directory, allows to navigate the directory tree in an easy manner, provides intuitive filtering capabilities and allows to select a file by selecting its name using arrow keys. Similar behaviour is provided when switching between opened buffers. A nice introductory tutorial to IDO can be found here.

How to enable:

(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(ido-mode 1)

Disable tabs for indentation

Description: We used to use tabs for indentation, but now we aim to have no tabs in the source code. There is a hook that will prevent you from pushing tabs into repository (unless file already contained tabs). This setting will prevent you from introducing tabs in the source code.

How to enable:

(setq-default indent-tabs-mode nil)

Highlight text beyond 80th column

Description: If you have a tendency to write too long lines of code this will help you by highlighting text beyond 80th column.

How to enable:

(require 'whitespace)
(setq whitespace-style '(face lines-tail))
(setq whitespace-line-column 80)
(global-whitespace-mode t)

Automatically removes trailing whitespaces when file is saved

Description: Currently source code of GHC contains lots of trailing whitespaces, which means that this setting is dangerous. It will remove ALL trailing whitespaces in every file that you edit, so you might have one or two lines changed by you and a hundred lines automatically changed by removing trailing whitespaces. This will require you to separate whitespaces into a separate commit by using git add -i. This is tedious, so be warned.

How to enable:

(add-hook 'before-save-hook 'delete-trailing-whitespace)

Highlight trailing whitespaces

Description: Automatic removal of trailing whitespaces described above can be a bit inconvenient to use. One of the alternative approaches to problem of trailing whitespaces is making them visible, so that you notice when you accidentally introduce them into a file. You will also see already existing trailing whitespaces. For more details see Useless Whitespace section of Emacs documentation.

How to enable:

(setq-default show-trailing-whitespace t)
(setq-default indicate-empty-lines t)

Kill whole line and newline with C-k if at beginning of line

Description: IF you place cursor at the beginning of line, the default behaviour of C-k is to fill all text until newline, but not the newline itself. You need to type C-k again to remove that newline. This setting allows to avoid typing C-k twice by killing all text until the newline and newline itself (but only when cursor is placed at the beginning of a line).

How to enable:

(setq kill-whole-line t)

Toggle line number display

Description: Allows you to toggle line number display with F11. For those who like to see line numbers at the beginning of each line.

How to enable:

(global-set-key (kbd "<f11>") 'global-linum-mode)

Always display column number in mode line

Description: By default Emacs only displays line number in the mode line. This setting adds column number.

How to enable:

(column-number-mode 1)

Switch between windows with Alt-<window number>

Description: Enhances switching between multiple windows. To switch to another window use pressing Alt-<window number>. Number of each window is displayed in red in the mode line.

How to enable: You need to install window-number extension and add this to your configuration:

(require 'window-number)

Switch to next/previous buffer with a single key

Description: This extension allows you to switch between next and previous buffer with F9 and F10 (by default). Emacs buffers (like *messages*) are ignored - use Shift-F9 and Shift-F10 to include them as well. Note that this extension maintains a list of buffers. After selecting a buffer it is moved to the beginning of the list. This means that buffers are ordered by the time of their recent use.

How to enable: You need to install cycle-buffer extension and add this to your configuration:

(autoload 'cycle-buffer                     "cycle-buffer"
  "Cycle forward." t)
(autoload 'cycle-buffer-backward            "cycle-buffer"
  "Cycle backward." t)
(autoload 'cycle-buffer-permissive          "cycle-buffer"
  "Cycle forward allowing *buffers*." t)
(autoload 'cycle-buffer-backward-permissive "cycle-buffer"
  "Cycle backward allowing *buffers*." t)
(autoload 'cycle-buffer-toggle-interesting  "cycle-buffer"
  "Toggle if this buffer will be considered." t)
(global-set-key [(f9)]        'cycle-buffer-backward)
(global-set-key [(f10)]       'cycle-buffer)
(global-set-key [(shift f9)]  'cycle-buffer-backward-permissive)
(global-set-key [(shift f10)] 'cycle-buffer-permissive)


Haskell mode

Description: Haskell mode is a major mode for Emacs. Features include: syntax highlighting, GHCi integration and automatic indentation. See online manual for details.

How to enable: See installation instructions on github.


Description: Extends haskell-mode. Features include: building on the fly with flymake, hlint integration, auto-completion of keywords. See here for a complete list.

How to enable: See installation instructions here.

Using tags to quickly locate definitions in a project

Description: Emacs can use a special index file, called "tags file", that stores locations of various definitions (functions, modules, data types) in a given directory. Once you've generated tags file (see installation instructions below) you can type M-. and enter name of identifier definition to jump to. Emacs by default jumps to identifier currently under the cursor.

How to enable: Begin by installing hasktags package from Hackage:

cabal install hasktags

Now go to the directory for which you want to generate tags and run:

hasktags --ignore-close-implementation .

In Emacs type M-x visit-tags-table and point to the generated TAGS.

Note on hasktags: hasktags program used to generate tags file has problems with correctly recognizing declarations of value constructors in a data type. It often mistakes pattern matching of the form (DataConstructor {}) as a data declaration and jumps to that pattern match instead of declaration.

Note on using tags for GHC sources:


Richard mentioned some interesting config on Newcomers page