What are the benefits of using indirect buffers? Under what circumstances are they useful?
To see and edit multiple locations of the same buffer. So, you don't have to scroll a long buffer but simply switch window to visible region. Each cloned buffer also has different mark ring, narrowing and other buffer properties. If you don't use indirect buffer but create another window of the same buffer, then everything is shared and you cannot perform other advance features beyond viewing and editing different locations. For example, with an indirect buffer, you can fold all the code block, effectively create a "tag tree" without affecting the original buffer. Using a window, both buffers in both windows are folded because both buffers are the same.
In sum, indirect buffer is a completely different buffer that happens to share the same text and file in your original buffer, so when you save cloned buffer, it saves your original file too.
One use case is using a different major mode on one part of a file.
For instance I was at one time working with xml documents with code inside certain elements. I wanted to be able to edit the entire document as if it were xml, but still have all the major mode features for the programming language embedded in certain elements, so I narrowed to the body of those elements in an indirect buffer that was running the appropriate major mode.
Here is a function from my config that I use to do this:
(defun indirect-region (start end) "Edit the current region in another indirect buffer. Prompt for a major mode to activate." (interactive "r") (let ((buffer-name (generate-new-buffer-name "*indirect*")) (mode (intern (completing-read "Mode: " (mapcar (lambda (e) (list (symbol-name e))) (apropos-internal "-mode$" 'commandp)) nil t)))) (pop-to-buffer (make-indirect-buffer (current-buffer) buffer-name)) (funcall mode) (narrow-to-region start end) (goto-char (point-min)) (shrink-window-if-larger-than-buffer)))
Note: this isn't perfect, things like font-lock require special additional work for use in this scenario.
Indirect buffers give you multiple views of the same buffer, in particular, multiple narrowings, each of which acts, in many respects, like a separate buffer.
As the Commentary for library
You can use indirect buffers for more than you might think. You can use clones taken from portions of Dired buffers, for example, to give you useful (active) views into a directory listing. There are only a few keys/commands (such as
gto update the listing) that do not work, because they depend on a view of the whole Dired buffer. Experiment, and you will no doubt find interesting new uses for indirect buffers.