I have a folder /var/~/
. In config .emacs
I wanna load some files from this folder.
I try to use (load-file "/var/~/foobar.el")
, but emacs alerts File error: Cannot open load file, ~/foobar.el
.
Furthermore I couldn't even open the files under this folder with c-x c-f
. In minibuffer the path will auto be redirected to my home.
How could I load files in that folder?

- 37,241
- 25
- 195
- 267

- 145
- 2
- 12
-
2Is that directory literally named `~` ? If so, that is not exactly a brilliant idea, since `~` tends to get expanded to the home directory in various contexts. – Paul R Sep 30 '14 at 16:57
-
Emacs jokes aside, this is about the use of a program, not making one. – MSalters Sep 30 '14 at 17:17
-
Paul R is correct -- rename your folder `~` to something else, as that character is generally recognized by Emacs as the path of the home directory. – lawlist Sep 30 '14 at 18:05
-
1Just out of curiosity: *Why did you name a directory `~`*? What made you think of doing that? I wonder if some doc somewhere is perhaps unclear and led you to do that. – Drew Sep 30 '14 at 22:14
-
1Because of CURIOSITY :) – Ophiuchus Hahn Oct 02 '14 at 11:22
2 Answers
You need to rename your directory.
load-file
is a simple wrapper around load
, which passes the given file name through substitute-in-file-name
. From the docstring of substitute-in-file-name
(emphasis mine):
Substitute environment variables referred to in FILENAME. `$FOO' where FOO is an environment variable name means to substitute the value of that variable. The variable name should be terminated with a character not a letter, digit or underscore; otherwise, enclose the entire variable name in braces.
If `/~' appears, all of FILENAME through that `/' is discarded. If `//' appears, everything up to and including the first of those `/' is discarded.
In other words, substitute-in-file-name
throws away everything before /~
, turning /var/~/foo.el
into ~/foo.el
.
I completely fail to see any reason in this behaviour, but it is what it is, and you cannot (easily) work around it, so renaming is your best way out of this dilemma.
It's a reasonable thing to do, anyway. Using ~
as directory name is bad idea on Unix systems generally, not just for Emacs alone.
-
-
1I believe the reason is to act as a shortcut to return to `~` or `/` (root dir). Otherwise the only way would be to backspace to root from any arbitrarily deep directory, then `/home/username/` to get back to `~`. – Jonathan Leech-Pepin Oct 01 '14 at 00:34
lunaryorn explained your problem well, and I agree with his suggestion that not using ~
in file paths is the best solution. However, If you can't rename these paths for whatever reason, I believe you can work around substitute-in-file-name
by load
ing a relative file path as documented here.
Basically, you need to add nil
to your load-path
variable, then set your default-directory
variable to the troublesome path, finally then load the file using a relative name. e.g.:
; adding nil causes load to also search your 'default-directory'
(setq load-path (append '(nil) load-path))
(setq default-directory "/tmp/~/")
(load "foobar.el")
Note that if you suspect the file name might exist (be loaded from) elsewhere in your load-path
you would need to ensure the file you want is first in the load-path
.

- 4,149
- 1
- 19
- 20