3

I have an init file on a USB drive. I use this USB drive at home and at work for my init file.

Until recently, my home and work computers were both Windows machines. I am now adding a Linux machine at home and am looking for a way for Emacs to play nicely with all of the machines: Windows and Linux.

Here is an example of the major problem I see:

(find-file "E:/emacs/org/gtd.org")

Linux doesn't use letter drives? What do I do?

3
  • Instead of having different init files, I like to query system-type at various locations in one master init.el which sets things differently depending upon what OS I am using.
    – lawlist
    Commented Nov 20, 2016 at 4:13
  • @lawlist could you give me an example? Commented Nov 21, 2016 at 14:02
  • For the example given you would simply define a variable containing the system-dependent directory and create the file-names using expand-filename.
    – politza
    Commented Nov 21, 2016 at 18:19

2 Answers 2

5

Make your configuration system or even machine dependant.

You can, for example, load system specific init files:

(load-file (expand-file-name
            (cond ((eq system-type 'windows-nt) "windows.el")
                  ((eq system-type 'gnu/linux) "linux.el")
                  (t "default-system.el"))
            user-emacs-directory))

To load machine depandent files you can use (system-name).

(defun load-with-warning (file)
  (when (file-exists-p file)
    (condition-case err
        (load-file file)
      (error (display-warning
              'init
              (format "loading file %s:\n\t%s" file (error-message-string err))
              :warning)))))

(load-with-warning (expand-file-name
            (system-name)
            user-emacs-directory))
2
  • Does this mean that I would have one init file point to other init files, depending on the machine and/or system? Commented Nov 21, 2016 at 14:01
  • Yes, exactly. Your init.el would reference windows.el, linux.el or default-system.el, depending on system type. If you also use the second example, than files named after your machines would be needed as well. Note, that you should gard load-file for the case you have no machine specific config file.
    – theldoria
    Commented Nov 22, 2016 at 15:02
0

This is the solution I made for myself:

(add-to-list 'load-path
     (concat (getenv "HOME") "/.emacs.d/conf.d/" 
         (prin1-to-string emacs-major-version) ))

(add-to-list 'load-path
     (concat (getenv "HOME") "/.emacs.d/conf.d/" 
         (replace-regexp-in-string "/" "-" (prin1-to-string system-type) )))

(add-to-list 'load-path
     (concat (getenv "HOME") "/.emacs.d/conf.d/" ))

This way I can load conf files depending OS and major versions (from v.23 to v.24 many changes had been introduced). This method only changes load path, then I use both (provide 'mylib)/(require 'mylib) to load what I need when I need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.