Emacs visits files in buffers. Editing changes the buffer first; saving writes its current contents to the associated pathname. Read minibuffer messages because permissions, conflicting disk changes, or other errors can prevent a write.
Advanced Text-Fu · Lesson 10
Emacs Manipulate Files
Learn how to visit, save, rename, revisit, and review file-backed buffers in Emacs.
Visiting a File
Use C-x C-f, which runs find-file, then enter a pathname in the minibuffer and press Enter:
C-x C-f
Emacs opens an existing readable file in a buffer or prepares a new file-visiting buffer when the pathname is missing. In the second case, no disk file exists until a save succeeds.
You can use Tab completion while entering a pathname. Visiting a directory normally opens Dired, Emacs's directory editor, rather than treating the directory as a text file.
Which Emacs key sequence prompts for a pathname and visits it?
When C-x C-f visits a pathname that does not exist, when is the disk file normally created?
Saving the Current Buffer
Use C-x C-s, which runs save-buffer, to save the current file-visiting buffer:
C-x C-s
If the buffer has no associated filename, Emacs prompts for one. A successful write clears the buffer's modified indicator; a failure leaves the unsaved data in the buffer and reports an error.
Which key sequence saves the current file-visiting buffer?
Writing under Another Name
Use C-x C-w, which runs write-file, to prompt for a pathname, write the buffer there, and make the buffer visit that new file:
C-x C-w
This is the Emacs “Save As” behavior. It differs from merely writing a separate copy while continuing to visit the original pathname.
Which key sequence performs the usual Save As operation for the current buffer?
Reviewing Several Modified Buffers
Use C-x s, which runs save-some-buffers, to examine modified file-visiting buffers:
C-x s
Emacs normally asks whether to save each eligible modified buffer. Read the buffer name and answer deliberately; this is not an unconditional save-all shortcut.
What does C-x s normally do?
Reverting from Disk
If a file changed on disk and you intentionally want to discard the buffer's current contents, run M-x revert-buffer and review the confirmation prompt. Reverting can destroy unsaved buffer edits, so use it only after confirming which source should win.
To compare before deciding, save a separate copy or use version-control and diff tools. Avoid treating reload operations as harmless when the buffer is modified.
Lesson complete
You finished Emacs Manipulate Files
You can now manage file-backed buffers without confusing visits and writes.
Visit a pathname with
C-x C-f.Create a missing file only when its buffer is saved.
Save the current buffer with
C-x C-s.Save under a new visited name with
C-x C-w.Review several modified buffers with
C-x s.
Keep your learning progress
Create a free account to save this lesson and continue learning on any device.
Create a free account