Blog Series Tags

The Configuration of Emacs

Emacs can only be configured naked by moonlight.

I kid, I kid – the moonlight is optional.

Emacs has a number of “modes”. Much has been said about them on the interwebs so I’ll take the easy way out and link to a such an article.

There are as you would have seen two kinds of modes, major modes and minor modes. Major modes change what Emacs believes is the content, for example there’s a major mode for C and a major mode for Python. Minor modes allow small changes in behavior in any content. CUA-Mode is a minor mode that does some trickery to allow C-c and C-v copy and paste like on the venerable Microsoft Word. It can be enabled by the command M-x followed by typing cua-mode in the mini buffer at the bottom of Emacs. However you don’t want to be manually typing that in every time you start emacs so you put some initialization settings in your Emacs init file.

On Windows systems Emacs looks for the .emacs init file in the directory denoted by the HOME environment variable. In case this is not present it looks for the init.el file in the [HOME]/.emacs.d/ folder. Set the HOME variable to the directory where you want your .emacs file to reside. Typically it’s a good idea not to put everything in your .emacs file but to put it in a couple of seperate .el files and include that from your .emacs file.

One convenient way to arrange your initialisation files is like so:

  1. [HOME]/.emacs
  2. [HOME]/.emacs.d/init.el
  3. [HOME]/.emacs.d/init-general.el
  4. [HOME]/.emacs.d/init-some-other-feature.el

This way you can put all your machine specific configurations in [1] and your cross machine root settings in [2] which you can then share across machines. The benefit you get from this setup is that even if [1] is not present Emacs will happily load [2] and do the general configuration. In [2] you load all the other files that you want to at startup. If you use Dropbox or a similar file sharing server to share your Emacs configuration files over the web across many machines that you work on this setup is specially useful as each machine will have machine specific .emacs files which can be minimal while the bulk of the settings would be in [2] and the files it loads in turn.

In your .emacs file you can load your init.el file like so:

(load "~/.emacs.d/init.el")

This tells Emacs to load the init file and execute it’s initialization calls. In the init file you can put this bit of elisp code to enable CUA mode taken from here.

;; CUA mode
(cua-mode t)
(setq cua-auto-tabify-rectangles nil) ;; Don't tabify after rectangle commands
(transient-mark-mode 1) ;; No region when it is not highlighted
(setq cua-keep-region-after-copy t) ;; Standard Windows behaviour

You can restart Emacs or run the configuration manually by pressing M-x (Press the ALT key and while it’s down press x) and typing load-file followed by the path to your .emacs file (~/.emacs).

Now you should find the behavior of Emacs a little more descent and Windows like. Pressing shift and selecting regions works as expected and so does C-c followed by C-v to paste things.

Emacs has a nice feature for cycling through copied text. If you copy multiple pieces of text Emacs allows you to cycle through then using C-v to do the initial paste and then M (Alt) – Y to cycle through the available pieces of copied text.

However C-y, normally reserved for undo in the windows world doesn’t work yet, it’s a problem we’ll address in the next post.

This is a post in the Emacs series.