Emacs: Different users, different themes, same init file

emacs

I use emacs at work, where I usually connect to the system with two different users:

  • myuser
  • commonuser

The former is my own personal user, whereas the latter is shared by the whole team to manage an application. (Irrelevant hint: This is a terrible idea. There are generally better, albeit a bit less obvious, ways to manage privileges. Take it from my experience).

In order to avoid unfortunate mix-ups, I want to define a separate theme for each user, and define it in their respective init files.

Everything else defined in the init file has to be shared by the two.

Simple solution

I create a separate init file /path/to/init/file/.emacs and created the following .emacs file in each user's home directory:

(load-file "/path/to/init/file/.emacs")
(load-theme my-favorite-theme)

This works like a charm. But of course, that would be too easy!

New solution needed

I was asked not to modify the .emacs file of commonuser. I won't go into the details of why, but the fact remains that I had to find a workaround. Here's what I got so far:

  1. Wrote a complete ~myuser/.emacs file, that I will load regardless of the user.
  2. Defined an alias for commonuser like this:

    alias emacs='emacs -Q --load ~myuser/.emacs'
    

This will make emacs ignore commonuser's init file and load myuser's.

Now my (simple) question is:

How can I make emacs behave differently according to the user launching it?

I am looking to do something similar:

if [[ $(whoami) == "myuser" ]]; then 
      (load-theme 'theme1)
if [[ $(whoami) == "commonuser" ]]; then 
      (load-theme 'theme2)

Best Answer

Emacs has variables that describe the current hostname and username. Just like you would do in order to conditionally do something based on the hostname, you have the variable user-login-name (from the docs: "The user's name, taken from environment variables if possible.").

So, I guess something like this would apply:

(when (string-equal user-login-name "myuser")
    (load-theme 'theme1))

(when (string-equal user-login-name "commonuser")
    (load-theme 'theme2))
Related Question