2

I work in an environment where we all log in as ourselves, then sudo to a common user. (Bleah). I'm starting to use emacs and would like to specify my own .emacs file at launch. What I think I want is to specify the location of .emacs with an environment variable, but I don't see any way to do that in the emacs documentation. Is there one?

As an alternative, perhaps I need to learn elisp and conditionally load my own file out of the common .emacs file located in /home/common_user/.emacs? In my case, I already have an environment variable SUDO_USER set to my name 'lcuff', and an environment variable MY_CONF set to /foo/bar/blah/lcuff, wherein I'd like to store my own .emacs file. How would I do this?

Thoughts and advice appreciated.

Leonard
  • 13,269
  • 9
  • 45
  • 72
  • I'm not sure if I understood your use case correctly, but would using alias be an alternative? I.e. `alias emacs="emacs --load file"` ? You could, of course, name the alias whatever you want in case anyone else use it aswell. – timss Apr 17 '13 at 23:05

4 Answers4

3

See:
C-hig (emacs) Find Init RET

Failing anything else, you can specify $HOME for a command with env:

env HOME=/foo/bar/blah/lcuff emacs
phils
  • 71,335
  • 11
  • 153
  • 198
2

Invoking emacs with the -u option will seek the init file for the given user, so

emacs -u Leonard

will run emacs with your emacs initialization file, even if you are logged in as another user.

R. P. Dillon
  • 2,780
  • 1
  • 18
  • 20
2

You can tell emacs to load the init file of your choice by saying:

emacs -q -l /path/to/my/preferred/.emacs
devnull
  • 118,548
  • 33
  • 236
  • 227
  • If Emacs doesn't know where the correct home directory is, that's going to be of limited use. – phils Apr 18 '13 at 06:02
  • @phils Moreover, when invoked with `-q` (or `-Q` for that matter) Emacs won't save anything. – yPhil Aug 06 '17 at 00:15
1

It's a little bit tricky working out who emacs thinks you are. From info:

More precisely, Emacs first determines which user's init file to use. It gets your user name from the environment variables LOGNAME and USER; if neither of those exists, it uses effective user-ID. If that user name matches the real user-ID, then Emacs uses `HOME'; otherwise, it looks up the home directory corresponding to that user name in the system's data base of users.

So I'd start first with finding out who emacs thinks you are, by starting emacs without loading any init files:

emacs -q

and then find out where emacs thinks your init file is:

(locate-user-emacs-file "yourrealusername")

I think on a fresh home environment it will default to ~/.emacs.d/yourusername, but that should help you decide where best to place the init file.

An important point is that it's pretty good practice to have an emacs user directory rather than just a startup file. There is a heap of extra guff that emacs needs to keep track of (package management, customized settings, backup files, dictionary, manually inserted lisp code etc etc) and I find it extremely useful to put all this stuff in the one spot. Emacs looks in ~/.emacs.d/init.el if it doesn't see ~/.emacs or ~/.emacs.el on the system. Make sure you don't have ~/.emacs still.

Create a new file called ~/.emacs.d/init.el

Put this in it:

(setq user-emacs-directory "~/.emacs.d/")
(message "This is my init.el file and noone elses!!!")
(inhibit-default-init) ;; there might be a default.el lurking somewhere

If emacs got your username and home right, you can then restart with:

emacs -u therealme

or, if it stuffs up the environment variables

env HOME=/this/is/my/home USER=blah emacs

There just might be a site-start.el somewhere that might get loaded before your init.el. If you suspect this, loading emacs with the --no-site-file option will nuke this.

tony day
  • 512
  • 2
  • 10