Blog Archives

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()
"Get current system's name"
(insert (format "%s" system-name))

;; Get current system type
(defun insert-system-type()
"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 ()
"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 ()
"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 ()
"Return true if the system we are running on is my PC at work"
(string-equal system-name "")

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 "")


Spanish MBP key modifiers for GNU emacs (dot emacs)

GNU Emacs (native version for Mac OS X) needs extra key bindings when used in a spanish keyboard Mac Book Pro. The following lines show how the main needed key bindings can be set up.

;; Key modifiers for Mac OS X in spanish MBP keyboard
(global-set-key (kbd "M-1") "|")
(global-set-key (kbd "M-2") "@")
(global-set-key (kbd "M-3") "#")
(global-set-key (kbd "M-º") "\\")
(global-set-key (kbd "M-ç") "}")
(global-set-key (kbd "M-+") "]")

Insert date string (dot emacs)

Need to add a common date string in your source code?

Add it to your .emacs!

;; Insert date string
(defun insert-date-string ()
"Insert a nicely formated date string."
(insert (format-time-string "%a %b %d %H:%M:%S %Y")))