Emacs Advent Calendar 08: Managing Buffers

Tue 2015-12-08

In Emacs, text is contained and edited inside buffers. Most buffers are created by visiting a file, others are used to display information or interact with external programs like the shell.

To switch to a buffer, use C-x b BUFFER <RET> (the usual Minibuffer commands work). If BUFFER does not exist, Emacs will create it. Note however that no file will be associated with this buffer (unlike when visiting a file with C-x C-f). If you try to save the buffer, Emacs will ask for a file name.

To cycle through buffers, use the C-x <LEFT> and C-x <RIGHT> commands. Here, C-x <LEFT> selects the previous buffer (the one that was selected last) while C-x <RIGHT> moves in the other direction.

Buffers are closed ("killed" in Emacs terminology) with C-x k BUFFER <RET>. The default argument is the current buffer. The command M-x kill-some-buffers loops through all buffers, offering to kill each one.

To display a list of all existing buffers, use C-x C-b. To only list buffers that are visiting files, use a C-u prefix: C-u C-x C-b. The *Buffer List* buffer offers special keybindings to quickly manipulate the list. Some basic ones to know are:

Key Action
d flag buffer for killing
s flag buffer for saving
u remove flags from buffer
n, <DOWN> move to next line
p, <UP> move to previous line
x save and/or kill all flagged buffers
<RET> select buffer
o select buffer in other window
S sort column at point
g refresh buffer list
q quit buffer list

For more information on how to deal with buffers, see sections 19.1 (Select Buffer), 19.2 (List Buffers), 19.4 (Kill Buffer) and 19.5 (Several Buffers) of the Emacs Reference Manual.

Tags:

This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.