1

Here are my settings:

  • Windows 10 64bit
  • GNU Emacs 25.2.1 (x86_64-w64-mingw32) of 2017-04-24

I want to keep the HOME environment variable to by user folder C:/User/chanyoungs for other programs but it seems like emacs needs the HOME to be at the emacs root folder which is C:/User/chanyoungs/Documents/Programming/emacs/. So my solution is to set the system environment variable as my user folder and have a .emacs file redirecting the environments in emacs as below:

(setq user-init-file "C:/Users/chanyoungs/Documents/Programming/emacs/.emacs.d/init.el")
(setq user-emacs-directory "C:/Users/chanyoungs/Documents/Programming/emacs/.emacs.d/")
(setq default-directory "C:/Users/chanyoungs/Documents/Programming/")
(setenv "HOME" "C:/Users/chanyoungs/Documents/Programming/emacs/")
(load user-init-file)

Then when I run emacs, it seems like init.el is ran but everything gets messed up. I found out that emacs thinks HOME is C:Users/chanyoungs/Documents/Programming/emacs/Documents/Programming/emacs/Documents/Programming/emacs/

I also did some tests to find out that I think basically emacs is unable to access any further than C:/Users/ i.e. it cannot access C:/Users/chanyoungs which is why it is unable to set HOME environment correctly.

So I tried placing the whole emacs folder in C:/ and changed the .emacs file to point everything to C:/ and this works.

The problem with this is I can no longer open any files in my documents which is a problem. If I keep the emacs folder in documents folder and set the Windows system environment HOME as the emacs folder then this does solve the emacs problem. It seems like emacs is able to access document folders if it's already in the document folder if that makes sense. But ideally, I want to keep the system HOME as my user root folder for other programs.

The strangest thing to all this is that it worked perfectly fine 2 days ago when I first set it up this way but now it's causing all these problems even though I didn't change any emacs settings since.

Any suggestions would be much appreciated. Thanks

2
  • One question: Why do you need HOME set differently outside Emacs? In my experience Windows does not use HOME so you should be able to change it for Emacs and other Unix tool ports that expect it.
    – glucas
    Commented May 8, 2017 at 15:08
  • Well, I was making ssh keys which was saved in HOME by default. This wasn't a problem by itself but it got me worried into thinking may be I'll have program conflicts in the future. But from reading what you explained for me, it looks like I don't have to worry.
    – chanyoungs
    Commented May 8, 2017 at 16:59

1 Answer 1

2

I'm not sure about the directory access problems, but I don't think you need to change HOME within Emacs. I have a setup to switch emacs configs with an environment variable, using a .emacs in HOME as you were trying:

(let
    ((dir (or (getenv "EMACS_USER_DIRECTORY") "~/.emacs.d")) 
  (setq user-emacs-directory (file-name-as-directory dir))
  (setq user-init-file (expand-file-name "init" user-emacs-directory))
  (load user-init-file))

This lets me specify a particular Emacs config by setting EMACS_USER_DIRECTORY before I start Emacs. In your case you can try just setting the dir such as:

(let
    ((dir "C:/User/chanyoungs/Documents/Programming/emacs")) 

Depending on your config you may find some other variables that need to be set prior to loading the init file, but most modern packages derive paths from user-emacs-directory already.

Note that the various Unix tool ports you are likely using with Emacs on Windows may also be writing files to HOME. This approach is only useful if you really want HOME and ~ to work consistently across Emacs and your shell or other tools. For example in my case I might load a different Emacs configuration directory but I still want to use my usual ~/org and ~/projects directories.

If instead you want a completely separate "Unix home" for your documents, rc files, Emacs config, and so on then you really should set HOME in your system environment variables. I've done this in the past and found that setting HOME does not interfere with other Windows tools. (Windows uses HOMEDRIVE, HOMEPATH, etc but not HOME).

8
  • Thanks so much for your answer glucas. Forgive me for my next question as I'm totally new to emacs. Did I need to modify any of the lines you gave to fit for me? I copied and pasted the codes in my .emacs in HOME but it doesn't seem to do anything.
    – chanyoungs
    Commented May 6, 2017 at 1:51
  • I updated the answer -- my solution defaults to HOME/.emacs.d but lets me set another variable to load a different config. You probably don't need that extra complexity.
    – glucas
    Commented May 6, 2017 at 1:57
  • Hi glucas. I tried to use your codes above like this (let ((dir "C:/Users/chanyoungs/Documents/Programming/emacs/.emacs.d")) (setq user-emacs-directory (file-name-as-directory dir)) (setq user-init-file (expand-file-name "init.el" user-emacs-directory)) (load user-init-file)) because my init.el is in .emacs.d but then it fails to run some codes: org-babel-load-file("c:/Users/chanyoungs/.emacs.d/myinit.org") because in init, the code is originally "~/.emacs.d/myinit.org". Emacs still thinks HOME is user root folder and I think this is still causing problems.
    – chanyoungs
    Commented May 6, 2017 at 2:39
  • If you're using ~/paths in your init they will point to HOME. You can change such paths to be relative to .emacs.d using e.g. (expand-file-name "myinit.org" user-emacs-directory). This makes your init.el not depend on what directory it is in.
    – glucas
    Commented May 6, 2017 at 2:45
  • I guess it's possible to go to my init codes and replace them as you suggested. I fear though that it seems like ~/ seems quite a standard way to give path references that it might conflict with any package codes I have should they contain any paths using ~/. Is that not going to be the case?
    – chanyoungs
    Commented May 6, 2017 at 2:53

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.