Most Emacs commands that operate on a file require you to specify the file name. (Saving and reverting are exceptions; the buffer knows which file name to use for them.) File names are specified in the minibuffer (see Minibuffer). Completion is available, to make it easier to specify long file names. See Completion.
There is always a default file name which is used if you enter an empty argument by typing just RET. Normally the default file name is the name of the file visited in the current buffer; this makes it easy to operate on that file with any of the Emacs file commands.
The syntax for accessing remote files unfortunately varies depending on the method used. The syntax for using FTP is ‘/user@remote-host:path-on-remote-host’. The syntax for using ssh is ‘/[user@remote-host]path-on-remote-host’.
In both cases the ‘user@’ portion is optional (it defaults to your local user name). path-on-remote-host may use the ‘~’ notation to indicate user’s home directory on the remote host. The default file name will reflect the remote host information.
Each buffer has a default directory, normally the same as the
directory of the file visited in that buffer. When Emacs reads a file
name, the default directory is used if you do not specify a directory.
If you specify a directory in a relative fashion, with a name that does
not start with a slash, it is interpreted with respect to the default
directory. The default directory of the current buffer is kept in the
variable default-directory
, which has a separate value in every
buffer. The value of the variable should end with a slash.
For example, if the default file name is /u/rms/gnu/gnu.tasks then the default directory is /u/rms/gnu/. If you type just ‘foo’, which does not specify a directory, it is short for /u/rms/gnu/foo. ‘../.login’ would stand for /u/rms/.login. ‘new/foo’ would stand for the filename /u/rms/gnu/new/foo.
When visiting a remote file via EFS or TRAMP, the remote directory becomes the default directory (see Visiting) for that buffer, just as a local directory would.
The variable default-directory-alist
takes an alist of major
modes and their opinions on default-directory
as a Lisp
expression to evaluate. A resulting value of nil
is ignored in
favor of default-directory
.
You can create a new directory with the function make-directory
,
which takes as an argument a file name string. The current directory is
displayed in the minibuffer when the function is called; you can delete
the old directory name and supply a new directory name. For example, if
the current directory is /u/rms/gnu, you can delete gnu
and type oryx and RET to create /u/rms/oryx.
Removing a directory is similar to creating one. To remove a directory,
use remove-directory
; it takes one argument, a file name string.
The command M-x pwd prints the current buffer’s default directory,
and the command M-x cd sets it (to a value read using the
minibuffer). A buffer’s default directory changes only when the cd
command is used. A file-visiting buffer’s default directory is initialized
to the directory of the file that is visited there. If a buffer is created
with C-x b, its default directory is copied from that of the
buffer that was current at the time.
The default directory name actually appears in the minibuffer when the
minibuffer becomes active to read a file name. This serves two
purposes: it shows you what the default is, so that you can type a
relative file name and know with certainty what it will mean, and it
allows you to edit the default to specify a different directory. To
inhibit the insertion of the default directory, set the variable
insert-default-directory
to nil
.
Note that it is legitimate to type an absolute file name after you enter the minibuffer, ignoring the presence of the default directory name. The final minibuffer contents may look invalid, but that is not so. See Minibuffer File.
‘$’ in a file name is used to substitute environment variables. For example, if you have used the shell command ‘setenv FOO rms/hacks’ to set up an environment variable named ‘FOO’, then you can use /u/$FOO/test.c or /u/${FOO}/test.c as an abbreviation for /u/rms/hacks/test.c. The environment variable name consists of all the alphanumeric characters after the ‘$’; alternatively, it may be enclosed in braces after the ‘$’. Note that the ‘setenv’ command affects Emacs only if done before Emacs is started.
To access a file with ‘$’ in its name, type ‘$$’. This pair
is converted to a single ‘$’ at the same time variable substitution
is performed for single ‘$’. The Lisp function that performs the
substitution is called substitute-in-file-name
. The substitution
is performed only on filenames read as such using the minibuffer.