In Emacs you can keep many files open at once. Each one lives in a buffer, and you view buffers through windows. In this lesson, C- means Ctrl and M- means Alt (Meta).
Opening files
Press C-x C-f (find-file). The minibuffer asks for a path:
Find file: ~/notes.txtPress Tab to complete names. If the file does not exist, Emacs gives you an empty buffer, and the file is created when you save.
Saving
C-x C-ssaves the current buffer.C-x C-wwrites it under a name you choose ("save as").C-x soffers to save each modified file buffer, asking about them one by one.
After a save, the echo area reports something like:
Wrote /home/sam/notes.txtAfter C-x C-w, the buffer is attached to the new name, so later saves go there.
To copy the contents of another file into the current buffer at the cursor, use C-x i.
Buffers
C-x bswitches to another buffer. Type its name, or pressEnterfor the suggested one.C-x C-blists all buffers.C-x kkills (closes) a buffer. It asks for confirmation if a file buffer has unsaved changes.
Killing a buffer does not delete the file on disk.
Windows
An Emacs "window" is a pane inside the screen.
C-x 2splits into one above the other.C-x 3splits side by side.C-x omoves to the other window.C-x 0closes the current window.C-x 1keeps only the current window.
Closing a window leaves its buffer open.
Extra files you may notice
By default Emacs often leaves a backup of the previous version, named with a trailing tilde, such as notes.txt~. While you have unsaved edits it may also write an auto-save file such as #notes.txt#. After a crash, M-x recover-file can use the auto-save file. Distributions and personal settings can change where these go.
Editing system files
Do not start the whole editor as root just to change one file. Use sudoedit, with the editor variable set to Emacs:
SUDO_EDITOR="emacs -nw" sudoedit /etc/hostsTry it
- Run
emacs -nw. - Press
C-x C-f, typeone.txt, add a line, and save withC-x C-s. - Open
two.txtthe same way. - Press
C-x 2, thenC-x o, thenC-x band pickone.txt. - Return to a single window with
C-x 1. - Exit with
C-x C-c.
For a live Linux shell, there is the files and links lab.
Check your understanding
Emacs Manipulate Files
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.