Single dot emacs file and per-computer configuration

1. Introduction

Once you have found the perfect emacs configuration stuff for you, you don’t usually want to change it. But it can happen that you want to use the same dot emacs configuration in different computers or even different systems.

I use emacs at home (Mac OS X and Ubuntu) and also at work (Kubuntu). It happens that when I write ChangeLog entries (C-x 4 a) I usually want to have different email addresses in the entries (my work email in the computers I use at work, and my GNU email in the computers I use at home). In the same way, I have a specific font type configuration for my GNU systems, but I keep the default one in my Mac O X emacs.

So basically you just need to detect different systems and computers, and apply specific configurations.

2. Get system type and system name

To detect different systems, you can use the ‘system-type‘ variable, and to detect different computers (with different hostnames, of course), you can use the ‘system-name‘ variable.

The following defuns show how you can know wich system type and name you have in each machine:

;; Get current system's name
(defun insert-system-name()
(interactive)
"Get current system's name"
(insert (format "%s" system-name))
)

;; Get current system type
(defun insert-system-type()
(interactive)
"Get current system type"
(insert (format "%s" system-type))
)

Those previous commands are interactive, so you can just type ‘M-x insert-system-name‘ or ‘M-x insert-system-type‘ to test them.

3. Configuration depending on the system type

In order to include system-type based configurations, you can define functions to check if the current system is the one you are looking for. Check these two functions to check if system is Mac OS X (darwin) or GNU (gnu/linux).

;; Check if system is Darwin/Mac OS X
(defun system-type-is-darwin ()
(interactive)
"Return true if system is darwin-based (Mac OS X)"
(string-equal system-type "darwin")
)

;; Check if system is GNU/Linux
(defun system-type-is-gnu ()
(interactive)
"Return true if system is GNU/Linux-based"
(string-equal system-type "gnu/linux")
)

Once you have these functions, you can put the system-dependent configuration:

;; disable closing emacs in Mac OS X
(if (system-type-is-darwin)
(global-unset-key "\C-z")
)

Note that ‘darwin‘ is the default system name for Mac OS X systems, and ‘gnu/linux‘ is the default system name for GNU/Linux systems.

3. Configuration depending on the system name

Now, you also want to put machine-dependent configuration, so you can define functions like this to detect in which machine you are currently located:

;; Check if the system is my Kubuntu GNU/Linux at work
(defun system-is-my-workpc ()
(interactive)
"Return true if the system we are running on is my PC at work"
(string-equal system-name "dev002.workplace.com")
)

And then you just need to use the previous function in the specific configurations (note that I’m using the hostname of my PC at work. Use your own hostname for you functions, dude).

;; For my machine in my worplace, setup my work email address
(if (system-is-my-workpc)
(setq user-mail-address "my.name@workplace.com")
)

Posted on September 28, 2008, in GNU Emacs and tagged , , . Bookmark the permalink. 4 Comments.

  1. This is excellent! Thanks!

  2. Exactly what I was looking for. Thank you for the instructions!

  3. Great post!! I was looking for something like that! Thanks a lot!

  4. Any idea how to make things dependent on the emacs version (e.g. emacs 23 vs emacs 22)?

Leave a comment