GNU Emacs Lisp Reference Manual
Expansion of a file name means converting a relative file name to an absolute one. Since this is done relative to a default directory, you must specify the default directory name as well as the file name to be expanded. Expansion also simplifies file names by eliminating redundancies such as `./' and `name/../'.
~'.) Otherwise, the current buffer's value of default-directory is used. For example: (expand-file-name "foo")
=> "/xcssun/users/rms/lewis/foo"
(expand-file-name "../foo")
=> "/xcssun/users/rms/foo"
(expand-file-name "foo" "/usr/spool/")
=> "/usr/spool/foo"
(expand-file-name "$HOME/foo")
=> "/xcssun/users/rms/lewis/$HOME/foo"
Filenames containing `.' or `..' are simplified to their canonical form:
(expand-file-name "bar/../foo")
=> "/xcssun/users/rms/lewis/foo"
`~/' is expanded into the user's home directory. A `/' or `~' following a `/' is taken to be the start of an absolute file name that overrides what precedes it, so everything before that `/' or `~' is deleted. For example:
(expand-file-name
"/a1/gnu//usr/local/lib/emacs/etc/MACHINES")
=> "/usr/local/lib/emacs/etc/MACHINES"
(expand-file-name "/a1/gnu/~/foo")
=> "/xcssun/users/rms/foo"
In both cases, `/a1/gnu/' is discarded because an absolute file name follows it.
Note that expand-file-name does not expand environment variables; only substitute-in-file-name does that.
(file-relative-name "/foo/bar" "/foo/")
=> "bar")
(file-relative-name "/foo/bar" "/hack/")
=> "/foo/bar")
~'. This variable is local in every buffer. expand-file-name uses the default directory when its second argument is nil.
On Unix systems, the value is always a string ending with a slash.
default-directory
=> "/user/lewis/manual/"
$' is the prefix to substitute an environment variable value. The environment variable name is the series of alphanumeric characters (including underscores) that follow the `$'. If the character following the `$' is a `{', then the variable name is everything up to the matching `}'.
Here we assume that the environment variable HOME, which holds the user's home directory name, has value `/xcssun/users/rms'.
(substitute-in-file-name "$HOME/foo")
=> "/xcssun/users/rms/foo"
If a `~' or a `/' appears following a `/', after substitution, everything before the following `/' is discarded:
(substitute-in-file-name "bar/~/foo")
=> "~/foo"
(substitute-in-file-name "/usr/local/$HOME/foo")
=> "/xcssun/users/rms/foo"
On VMS, `$' substitution is not done, so this function does nothing on VMS except discard superfluous initial components as shown above.