Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท Editing and Buffers

Buffers and Ibuffer

Work with many buffers: switch with C-x b, list with C-x C-b and Ibuffer, kill and rename buffers, understand special buffers like *scratch* and *Messages*, and read-only mode.

A buffer is the basic unit of Emacs: a named chunk of text in memory. Files are loaded into buffers, but so are directory listings, command output, the manual, a shell session and your notes. At any time an Emacs session may hold dozens of buffers, and you move between them instead of opening and closing programs. Handling them fluently is what makes Emacs feel like one workspace.

Switching and listing

Keys Command Effect
C-x b switch-to-buffer Switch by name, with completion; the default is the last buffer you used
C-x C-b list-buffers Show a list of all buffers in a window
C-x <left> / C-x <right> previous-buffer / next-buffer Cycle in order
C-x 4 b switch-to-buffer-other-window Show another buffer in another window
C-x k kill-buffer Close a buffer (the default is the current one)
C-x C-q read-only-mode Toggle read-only for the buffer
M-x rename-buffer Give the buffer a new name (the file is not renamed)

C-x b RET with nothing typed switches to the previous buffer, so C-x b RET toggles between two buffers like Alt-Tab. Typing part of a name and pressing TAB completes it. If you enter a name that does not exist, Emacs creates a new empty buffer by that name.

Killing buffers

Killing a buffer removes it from memory; it does not delete the file. If the buffer has unsaved changes Emacs asks first. A long session slowly collects buffers, so clean up now and then: M-x kill-some-buffers offers each buffer in turn, and M-x clean-buffer-list closes old unused ones automatically. Some people turn on midnight-mode for that.

JS Stand-in: a buffer list ordered by recent use

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

No output.

The buffer menu: C-x C-b

C-x C-b shows a buffer list. In it, the columns are: . for the current buffer, * for modified, % for read-only, then the name, size, mode and file. Move with C-n and C-p, then use these keys:

  • RET or f visits the buffer; o shows it in another window.
  • d or k marks it for deletion; s marks it to be saved; u unmarks.
  • x executes the marks.
  • ~ marks it unmodified.

Ibuffer: a better list

M-x ibuffer is a more capable buffer list that ships with Emacs, and many people rebind C-x C-b to it: (global-set-key (kbd "C-x C-b") #'ibuffer). It supports:

Key Effect
m / u Mark / unmark the buffer on this line
D Kill the marked buffers
S Save the marked buffers
/ m Filter by major mode
/ n Filter by name (regexp)
/ / Remove all filters
s a / s v Sort by name / by recency
` Switch between filter-group sets

With filter groups you can show the list divided into sections, for instance one for Org files, one for code and one for Emacs's internal buffers. Marks act on all marked buffers at once, so "kill every Dired buffer" is one filter plus m for all of them, then D.

JS Stand-in: filter and mark buffers by mode

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

No output.

Special buffers

Buffer names in *stars* are conventionally created by Emacs rather than visiting files:

  • *scratch*: Lisp experiments, not saved.
  • *Messages*: the log of echo-area messages (C-h e).
  • *Help*, *Completions*, *Occur*, *grep*, *compilation*: output and tool windows.
  • Names starting with a space are hidden from the buffer list, an internal convention.

Press q in most of these to close the window. They are normal buffers, so you can switch to them, search in them and copy text out of them.

Read-only and modified flags

A buffer is modified (**) after you edit it and read-only (%%) if protected. Files you do not have permission to change are opened read-only automatically. C-x C-q toggles read-only mode on and off. When viewing a file you do not want to change by accident, M-x view-mode lets you scroll with SPC and DEL and leave with q.

Several buffers for one file

C-x 4 c (clone-indirect-buffer-other-window) creates an indirect buffer sharing the same text, so you can see two parts of a huge file in separate windows with different narrowing. Related: narrowing (C-x n n to narrow to the region, C-x n w to widen) hides everything except the part you are working on. It also helps when you apply a search and replace to part of a buffer.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which key switches to another buffer by name?

    C-x b is switch-to-buffer; C-x C-f opens files.
  2. 2. Does killing a buffer delete its file?

    Killing a buffer closes it in Emacs; the file on disk is untouched.
  3. 3. What does * in the buffer list mean?

    An asterisk in the modified column marks unsaved changes.
  4. 4. Which command is a more capable replacement for list-buffers?

    Ibuffer adds filtering, grouping, sorting and bulk operations.

Summary

  • C-x b switches (with completion), C-x k kills, C-x C-b lists; C-x b RET jumps to the previous buffer.
  • Killing a buffer never deletes the file; unsaved changes trigger a prompt.
  • Ibuffer (M-x ibuffer) filters, groups, sorts and bulk-edits buffers.
  • Buffers named *like this* are Emacs's own; C-x C-q toggles read-only; C-x n n narrows.

Next: Windows and Frames