ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

22.1 File Names

Many Emacs commands that operate on a file require you to specify the file name, using the minibuffer (see Minibuffer). You can use completion to specify long file names (see Completion). Note that file name completion ignores file names whose extensions appear in the variable completion-ignored-extensions (see Completion Options).

For most operations, there is a default file name which is used if you type just <RET> to enter an empty argument. Normally, the default file name is the name of the file visited in the current buffer.

Each buffer has a default directory which is normally the same as the directory of the file visited in that buffer. For example, if the default file name is /u/rms/gnu/gnu.tasks, the default directory is normally /u/rms/gnu/. The default directory is kept in the variable default-directory, which has a separate value in every buffer. When a command reads a file name using the minibuffer, the default directory usually serves as the initial contents of the minibuffer. To inhibit the insertion of the default directory, set the variable insert-default-directory to nil.

If you enter a file name without a directory, that specifies a file in the default 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. For example, suppose the default directory is /u/rms/gnu/. Entering just ‘foo’ in the minibuffer, with a directory omitted, specifies the file /u/rms/gnu/foo; entering ‘../.login’ specifies /u/rms/.login; and entering ‘new/foo’ specifies /u/rms/gnu/new/foo.

When typing a file name into the minibuffer, you can make use of a couple of shortcuts: a double slash is interpreted as “ignore everything before the second slash in the pair,” and ‘~/’ is interpreted as your home directory. See Minibuffer File, for more information about these shortcuts.

The command M-x pwd displays the 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 it visits. If you create a buffer with C-x b, its default directory is copied from that of the buffer that was current at the time (see Select Buffer).

The character ‘$’ is used to substitute an environment variable into a file name. The name of the environment variable consists of all the alphanumeric characters after the ‘$’; alternatively, it can be enclosed in braces after the ‘$’. For example, if you have used the shell command export FOO=rms/hacks to set up an environment variable named FOO, then both /u/$FOO/test.c and /u/${FOO}/test.c are abbreviations for /u/rms/hacks/test.c. If the environment variable is not defined, no substitution occurs, so that the character ‘$’ stands for itself.

Note that environment variables affect Emacs only if they are applied before Emacs is started.

To access a file with ‘$’ in its name, if the ‘$’ causes expansion, type ‘$$’. This pair is converted to a single ‘$’ at the same time that variable substitution is performed for a single ‘$’. Alternatively, quote the whole file name with ‘/:’ (see Quoted File Names). File names which begin with a literal ‘~’ should also be quoted with ‘/:’.

You can include non-ASCII characters in file names if you set the variable file-name-coding-system to a non-nil value. See File Name Coding.

blog comments powered by Disqus