Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 38 ยท Basics and Navigation

Files, Saving and Quitting

Open and save files in Emacs with find-file, save-buffer, write-file and save-some-buffers. Understand backups, auto-save, recover-this-file and how to quit safely.

In Emacs you never edit a file directly. You visit it: Emacs reads the file into a buffer, you edit the buffer, and you save the buffer back to the file. Understanding this model explains the commands, the backup files and the "modified" flag in the mode line.

Visiting files

Keys Command Effect
C-x C-f find-file Open a file (or create a new one if the name does not exist)
C-x C-r find-file-read-only Open without allowing edits
C-x 4 f find-file-other-window Open in another window
C-x C-v find-alternate-file Replace the current buffer with another file (fix a typo in a name)
C-x i insert-file Insert a file's contents at point

C-x C-f asks for a file name in the minibuffer. The prompt starts with the current directory. Use TAB to complete names, RET to accept, and C-g to cancel. If you type a name that does not exist, Emacs opens an empty buffer; the file is created when you first save. You can type a directory name to open Dired, the directory editor, which later lessons cover.

Handy tricks in the file prompt:

  • ~/ jumps to your home directory. Typing / after a name restarts the path: ~/projects//etc/hosts means /etc/hosts, which is useful when a long path is pre-filled.
  • Type /ssh:user@host:/path to open a remote file through TRAMP, Emacs's remote-file layer, if SSH is set up.
  • M-n in the prompt can insert the file name at point, M-p goes back through history.

Saving

Keys Command Effect
C-x C-s save-buffer Save the buffer to its file
C-x C-w write-file Save under a new name ("Save As")
C-x s save-some-buffers Ask about every modified file buffer

The mode line shows ** when a buffer has unsaved changes and -- when it matches the file. If you press C-x C-s on an unmodified buffer, Emacs says "(No changes need to be saved)" and does nothing.

If the file changed on disk after you opened it, Emacs warns you before overwriting: it asks whether to save anyway. To reload a file from disk after an outside change run M-x revert-buffer. For files that change often, such as logs, M-x auto-revert-mode (or global-auto-revert-mode) refreshes them automatically.

JS Stand-in: the visit and save model

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Backups and auto-save

Emacs is careful with your data and creates two kinds of extra files:

  • Backup files end with ~ (for example notes.txt~). The first time you save a buffer in a session, Emacs copies the old file to this name. By default the backup is made in the same directory, which clutters projects. Many users send backups elsewhere: (setq backup-directory-alist '(("." . "~/.emacs.d/backups"))).
  • Auto-save files are named with hash signs, #notes.txt#. Emacs writes the unsaved buffer to this file every 300 typed characters or 30 seconds of idle time (the auto-save-interval and auto-save-timeout variables), and deletes it when you save properly.

After a crash, reopen the file and run M-x recover-this-file, which offers to restore your work from the auto-save file. Lock files (.#notes.txt, a symbolic link) prevent two Emacs sessions from editing the same file at once; the "file is locked" warning is Emacs protecting you.

JS Stand-in: backup and auto-save file names

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Quitting

Keys Command Effect
C-x C-c save-buffers-kill-terminal Offer to save modified buffers, then exit Emacs
C-z suspend-frame Minimize the frame (GUI) or suspend (terminal); the shell's fg resumes
C-x k kill-buffer Close one buffer, not Emacs

When quitting with unsaved buffers, Emacs lists them and asks y/n for each. Press C-g to abort the quit. Many Emacs users never quit at all: they keep one long-running session, a habit that works well with the daemon described in the installation lesson.

Opening several files from the shell

emacs a.txt b.txt opens both: the first in the main window, the second in a buffer you reach with C-x b. The buffers lesson covers switching.

Recent files

Turn on recentf-mode (M-x recentf-mode) and call M-x recentf-open to choose from files you opened lately. Put (recentf-mode 1) in your init file to keep it permanent.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens when you edit a visited file in Emacs?

    You edit the buffer; the file changes only when you save.
  2. 2. Which key saves a buffer under a new name?

    C-x C-w is write-file, the equivalent of Save As.
  3. 3. What does a file named #notes.txt# represent?

    Auto-save files are named with hash signs; backups end with a tilde.
  4. 4. How do you restore work after a crash?

    Open the file and run recover-this-file to restore from the auto-save file.

Summary

  • C-x C-f visits a file; C-x C-s saves; C-x C-w saves under a new name; C-x s saves all modified buffers.
  • ** in the mode line means unsaved changes.
  • Backups end in ~; auto-saves look like #name#; M-x recover-this-file uses them.
  • C-x C-c quits Emacs after offering to save; C-x k closes only a buffer.

Next: Incremental Search and Query Replace