2

I am looking for method to use two different config files - init files. First config: when I am coding in full Emacs windows (open file as emacs namefile this define ~/.emacs_conf2) Second: when I would fast open file (without all packages, only some shortcuts etc) into terminal (commend- emacs namefile - nw this define ~/.emacs_conf2)?

Thanks for answer and explanation of your methods, regards!

MichalSzczep
  • 141
  • 5
  • I guess, you don't want to start Emacs using `emacs -q -l ~/.emacs_config1` & `emacs -q -l ~/.emacs_config2`? – Nsukami _ Jan 07 '17 at 16:45
  • Thanks, I see your point of view, I would like have it automatic if I have emacs namefile -nw this define ~/.emacs_conf1 if I open emacs namefile this define ~/.emacs_conf2 – MichalSzczep Jan 07 '17 at 16:57
  • 2
    In that case you can simply check if you're using GUI with `(display-graphic-p)` and then load the full config or a cut down version of it for the terminal. –  Jan 07 '17 at 17:00
  • 3
    Are you fundamentally opposed to having certain packages loaded, or is speed the main concern? Put another way: there's a way to get emacs to load muchmuch faster for editing small files, say in the terminal (using emacsclient); do you know about that? Is two configs what you absolutely need, or just a fast and a slow way to load emacs? – Gastove Jan 07 '17 at 17:01
  • @Gastove thank you, I am not sure that I understand. Main goal is to open emacs faster as you said. However I would have some of my config lines. Can you go on more details? – MichalSzczep Jan 07 '17 at 17:08
  • @DoMiNeLa10 Thank you I will probably solve it like this. – MichalSzczep Jan 07 '17 at 17:13
  • So basically, you want Emacs to know programmatically in your configuration if the command-line options contain `nw` -- if so, then load file `emacs_conf2`, else `emacs-conf1` or perhaps no configuration? See `command-line-args` is a variable defined in 'C source code'. *Args passed by shell to Emacs, as a list of strings. Many arguments are deleted from the list as they are processed*. – lawlist Jan 07 '17 at 17:13
  • Ah . . . I just tested the `command-line-args` idea and it seems that Emacs deletes `-nw` from the list of saved arguments before processing the `.emacs`. So while I understand the question, I don't have the solution . . . but perhaps someone else does. I have not tested `command-line-functions`, but that may be another idea to explore. Here is the link to the manual: https://www.gnu.org/software/emacs/manual/html_node/elisp/Command_002dLine-Arguments.html – lawlist Jan 07 '17 at 17:26
  • The following snippet seems to do the trick: `(if initial-window-system (message "This IS an 'initial-window-system'.") (message "There is NOT an 'initial-window-system'."))` – lawlist Jan 07 '17 at 20:19

3 Answers3

4

As I can see from one of your comments, the main point is to run Emacs faster in some cases. Then there are several options for you.

  1. You can load your GUI-related configuration only when you run Emacs in GUI mode. You can use something like the following code (just place your GUI-related configuration in init_gui.el):

    (when window-system
      (load "~/.emacs.d/init_gui.el"))
    

NOTE: load function should get the full path to your init_gui.el, otherwise it will search for the file in your load-path. Of course, it is also possible to put the location of you init_gui.el to load-path with (add-to-list 'load-path "path_to_the_directory_with_your_file").

  1. You can autorun Emacs server and then just use emacsclient to connect to it. (As far as I know Emacs server will not work on Windows.) It will be quite fast even in GUI mode. In this case you need to put the following code to you configuration file:

    (require 'server)
    (unless (server-running-p)
      (server-start))
    

Also, you can look at use-package, which can help you "to isolate package configuration in your .emacs file".

  • where should I keep the init_gui.el? I tried to storage it in next to ~/.emacs and in ~/.emacs.d/ . Both doesn't work, what do I do wrong? thanks for your help – MichalSzczep Jan 08 '17 at 15:50
  • 1
    You probably want the file name in the load to specify the full name. e.g. ""~/init_gui.el" or "~/.emacs.d/int_gui.el", or whatever (I'd've called it "~/.emacs_gui.el" myself, or actually because of my more complex setup, it'd be "~/.../emacs/gui.elc") – MAP Jan 08 '17 at 23:59
  • @MichalSzczep, I've just updated my answer. As @MAP put it `load` gets full path to the file. – Lev Lamberov Jan 10 '17 at 09:04
  • 1
    It would make sense to put it in `~/.emacs.d/` and use `(load (locate-user-emacs-file "init_gui.el"))` to load it. – YoungFrog Jan 10 '17 at 10:33
  • @LevLamberov great, thanks for your help, one more time! – MichalSzczep Jan 11 '17 at 08:19
3

You can achieve this "fast load, slow load" using the server/client functionality of emacs, which works like this:

Emacs can be started as a server. This can either be done as a stand-alone, running a server in the background. Or, if you want to, say, run a main GUI instance of emacs, the GUI and server can be started at the same time.

Once the server is started, it can be connected to using the emacsclient application, which will connect to the presently running emacs server. This is vastly faster to activate than starting emacs from scratch.

To do this:

  1. To start emacs as a server, in the background, in your terminal, issue emacs --daemon.
  2. To start the emacs server while you load emacs -- for instance, if you're using the emacs GUI, add (server-start) to your init.el (or init.el equivalent).
  3. To connect to the server, use emacsclient. To force emacsclient to open the file in question in the terminal, use the -t argument. E.g.: emacsclient -t foo.txt.

If you run Emacs as a server, you might also appreciate having a command to stop running it: emacsclient -e '(kill-emacs)'.

Gastove
  • 1,511
  • 9
  • 15
0

FWIW, here's what I do:

in ~/.emacs I define a function full-fledged-emacs which has settings which only apply to when I start my main Emacs session.

So if I start a "normal" emacs <file> those settings aren't applied. And when I want to start my main Emacs session, I do emacs -f full-fledged-emacs.

Stefan
  • 26,154
  • 3
  • 46
  • 84