is there a way to specify custom init file on emacs startup?
Asked Answered
Z

4

35

I've tried runemacs --user d:\path\to\init\.emacs but it gives

Error (initialization): Invalid user name d:\path\to\init\.emacs

I mean custom file in custom location like on my pendrive etc. and not in my default locations in user folder. I try to create a cmd files with corresponding inits for different configuration.

Zama answered 17/6, 2013 at 11:51 Comment(1)
Invalid user name should give a hint!Hoick
S
45

I am guessing that your end goal is to run Emacs from a USB drive which you can carry around. There are two ways to have Emacs start with a different init file:

(1) the -q --load way

This is simply to run

emacs -q --load path-to-your-init.el

which starts Emacs without any initialization, then loads path-to-your-init.el

Gotchas of this way: in this case, Emacs doesn't really consider the el file to be an init file, in fact, Emacs hasn't gone through initialization at all, which means the following post-initialization steps are skipped:

  • enabling packages, that is, evaluating (package-initialize)

  • running hook after-init-hook, that is, evaluating (run-hooks after-init-hook)

You may have to add post-initialization stuff in your alternate init file.

Also, Customize interface won't let you save settings if Emacs is started with -q option.

(2) the HOME way

Create my-emacs.bat with contents:

set HOME=%~dp0
C:\path-to-your\Emacs\bin\runemacs.exe --xrm "emacs.Background: light green"

When you click on that bat file, Emacs creates folder named .emacs.d not in your usual user directory, but in where the bat file is. You can put your own init.el in that .emacs.d folder. With this Emacs, evaluating (find-file "~/.emacs.d/init.el") opens your own portable init.el and not the init file in your usual user directory. You may want to delete the --xrm "emacs.Background: light green" part, I include that because I always forget which Emacs is which.

Gotchas: this Emacs has its own package directory in its own .emacs.d directory. That may or may not be what you want. If you want to share the package directory, you can change package-user-dir or create a symbolic link to the to-be-shared package directory using Link Shell Extension.

Somethings to consider for running Emacs on a USB drive:

Emacs outsources some of its features to external tools. Grepping and comparison are outsourced to grep and diff, so you probably have installed at least GnuWin32 Grep and GnuWin32 DiffUtils on your system. And part of spell checking is outsourced to aspell, which is also a separate install. Encryption is outsourced to GPG, so Gpg4Win another separate install. For your portable Emacs to have all those features, all those external tools should be on your USB drive as well. Fortunately, GnuWin32 and aspell are portable-friendly. I don't know if GPG is. The dependency to external grep may be eliminated if you manage to make versions of lgrep and rgrep that only depend on eshell's own grep, but it seems nobody has done it, so making such lgrep/rgrep is to boldly go where no man has gone before.

Stearin answered 17/6, 2013 at 13:43 Comment(2)
Wonderful, still working perfectly in 2020. Thank you.Piranha
One can set the user-init-file to enable customization save. emacs -Q -T FrameTitle --eval "(setq user-init-file \"$INITFILE\")" -l $INITFILE )Ostmark
R
12

You can get help on all of the emacs options by typing emacs --help on the command line. That help shows that --user expect the name of a user, not the path to an elisp file.

To load a lisp file at startup you can use the --load option:

$ emacs --help
...
--load, -l FILE         load Emacs Lisp FILE using the load function
...

If you want to prevent your default init file from loading, you can use this option:

$ emacs --help
...
--no-init-file, -q          load neither ~/.emacs nor default.el
...
Racialism answered 17/6, 2013 at 11:59 Comment(2)
Which all begs the question: why in 2022 isn't there a simple option --init DIR where I can point at different .emacs.d directories? Sigh.Doyledoyley
@Doyledoyley There is ... --init-directory=DIR use DIR when looking for the Emacs init files.Repairman
A
2

As of Emacs29 there is the --init-directory flag to specify another init directory where you can place your custom init file in.

Anabolism answered 3/12, 2023 at 12:2 Comment(0)
H
1

I use the following I pulled from daviwil repo:
https://github.com/daviwil/emacs-from-scratch/tree/9388cf6ecd9b44c430867a5c3dad5f050fdc0ee1.

I start emacs using emacs -Q --load tweaks.el --load init.el init.el.

My teaks.el sets the emacs packages directory using the following:
(setq package-user-dir "~/emacs_test/packages")

Hartebeest answered 14/4, 2023 at 14:22 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Phocis

© 2022 - 2024 — McMap. All rights reserved.