[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

M.2 Visiting Files

C-x C-f
Visit a file (find-file).
C-x C-r
Visit a file for viewing, without allowing changes to it (find-file-read-only).
C-x C-v
Visit a different file instead of the one visited last (find-alternate-file).
C-x 4 f
Visit a file, in another window (find-file-other-window). Don't alter what is displayed in the selected window.
C-x 5 f
Visit a file, in a new frame (find-file-other-frame). Don't alter what is displayed in the selected frame.
M-x find-file-literally
Visit a file with no conversion of the contents.

Visiting a file means copying its contents into an Emacs buffer so you can edit them. Emacs makes a new buffer for each file that you visit. We often say that this buffer "is visiting" that file, or that the buffer's "visited file" is that file. Emacs constructs the buffer name from the file name by throwing away the directory, keeping just the name proper. For example, a file named `/usr/rms/emacs.tex' would get a buffer named `emacs.tex'. If there is already a buffer with that name, Emacs constructs a unique name--the normal method is to append `<2>', `<3>', and so on, but you can select other methods (see section N.7.1 Making Buffer Names Unique).

Each window's mode line shows the name of the buffer that is being displayed in that window, so you can always tell what buffer you are editing.

The changes you make with editing commands are made in the Emacs buffer. They do not take effect in the file that you visited, or any place permanent, until you save the buffer. Saving the buffer means that Emacs writes the current contents of the buffer into its visited file. See section M.3 Saving Files.

If a buffer contains changes that have not been saved, we say the buffer is modified. This is important because it implies that some changes will be lost if the buffer is not saved. The mode line displays two stars near the left margin to indicate that the buffer is modified.

To visit a file, use the command C-x C-f (find-file). Follow the command with the name of the file you wish to visit, terminated by a RET.

The file name is read using the minibuffer (see section E. The Minibuffer), with defaulting and completion in the standard manner (see section M.1 File Names). While in the minibuffer, you can abort C-x C-f by typing C-g. File-name completion ignores certain filenames; for more about this, see E.3.4 Completion Options.

When Emacs is built with a suitable GUI toolkit, it pops up the standard File Selection dialog of that toolkit instead of prompting for the file name in the minibuffer. On Unix and GNU/Linux platforms, Emacs does that when built with LessTif and Motif toolkits; on MS-Windows, the GUI version does that by default.

Your confirmation that C-x C-f has completed successfully is the appearance of new text on the screen and a new buffer name in the mode line. If the specified file does not exist and could not be created, or cannot be read, then you get an error, with an error message displayed in the echo area.

If you visit a file that is already in Emacs, C-x C-f does not make another copy. It selects the existing buffer containing that file. However, before doing so, it checks that the file itself has not changed since you visited or saved it last. If the file has changed, a warning message is shown. See section Simultaneous Editing.

Since Emacs reads the visited file in its entirety, files whose size is larger than the maximum Emacs buffer size (see section N. Using Multiple Buffers) cannot be visited; if you try, Emacs will display an error message saying that the maximum buffer size has been exceeded.

What if you want to create a new file? Just visit it. Emacs displays `(New file)' in the echo area, but in other respects behaves as if you had visited an existing empty file. If you make any changes and save them, the file is created.

Emacs recognizes from the contents of a file which convention it uses to separate lines--newline (used on GNU/Linux and on Unix), carriage-return linefeed (used on Microsoft systems), or just carriage-return (used on the Macintosh)---and automatically converts the contents to the normal Emacs convention, which is that the newline character separates lines. This is a part of the general feature of coding system conversion (see section Q.7 Coding Systems), and makes it possible to edit files imported from different operating systems with equal convenience. If you change the text and save the file, Emacs performs the inverse conversion, changing newlines back into carriage-return linefeed or just carriage-return if appropriate.

If the file you specify is actually a directory, C-x C-f invokes Dired, the Emacs directory browser, so that you can "edit" the contents of the directory (see section AB. Dired, the Directory Editor). Dired is a convenient way to delete, look at, or operate on the files in the directory. However, if the variable find-file-run-dired is nil, then it is an error to try to visit a directory.

Files which are actually collections of other files, or file archives, are visited in special modes which invoke a Dired-like environment to allow operations on archive members. See section M.12 File Archives, for more about these features.

If the file name you specify contains shell-style wildcard characters, Emacs visits all the files that match it. Wildcards include `?', `*', and `[...]' sequences. See section M.14 Quoted File Names, for information on how to visit a file whose name actually contains wildcard characters. You can disable the wildcard feature by customizing find-file-wildcards.

If you visit a file that the operating system won't let you modify, Emacs makes the buffer read-only, so that you won't go ahead and make changes that you'll have trouble saving afterward. You can make the buffer writable with C-x C-q (vc-toggle-read-only). See section N.3 Miscellaneous Buffer Operations.

Occasionally you might want to visit a file as read-only in order to protect yourself from entering changes accidentally; do so by visiting the file with the command C-x C-r (find-file-read-only).

If you visit a nonexistent file unintentionally (because you typed the wrong file name), use the C-x C-v command (find-alternate-file) to visit the file you really wanted. C-x C-v is similar to C-x C-f, but it kills the current buffer (after first offering to save it if it is modified). When C-x C-v reads the file name to visit, it inserts the entire default file name in the buffer, with point just after the directory part; this is convenient if you made a slight error in typing the name.

If you find a file which exists but cannot be read, C-x C-f signals an error.

C-x 4 f (find-file-other-window) is like C-x C-f except that the buffer containing the specified file is selected in another window. The window that was selected before C-x 4 f continues to show the same buffer it was already showing. If this command is used when only one window is being displayed, that window is split in two, with one window showing the same buffer as before, and the other one showing the newly requested file. See section O. Multiple Windows.

C-x 5 f (find-file-other-frame) is similar, but opens a new frame, or makes visible any existing frame showing the file you seek. This feature is available only when you are using a window system. See section P. Frames and X Windows.

If you wish to edit a file as a sequence of ASCII characters with no special encoding or conversion, use the M-x find-file-literally command. It visits a file, like C-x C-f, but does not do format conversion (see section T.11 Editing Formatted Text), character code conversion (see section Q.7 Coding Systems), or automatic uncompression (see section M.11 Accessing Compressed Files), and does not add a final newline because of require-final-newline. If you already have visited the same file in the usual (non-literal) manner, this command asks you whether to visit it literally instead.

Two special hook variables allow extensions to modify the operation of visiting files. Visiting a file that does not exist runs the functions in the list find-file-not-found-hooks; this variable holds a list of functions, and the functions are called one by one (with no arguments) until one of them returns non-nil. This is not a normal hook, and the name ends in `-hooks' rather than `-hook' to indicate that fact.

Successful visiting of any file, whether existing or not, calls the functions in the list find-file-hooks, with no arguments. This variable is really a normal hook, but it has an abnormal name for historical compatibility. In the case of a nonexistent file, the find-file-not-found-hooks are run first. See section AD.2.3 Hooks.

There are several ways to specify automatically the major mode for editing the file (see section R.1 How Major Modes are Chosen), and to specify local variables defined for that file (see section AD.2.5 Local Variables in Files).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on April 2, 2002 using texi2html