20

What are the benefits of using indirect buffers? Under what circumstances are they useful?

3 Answers 3

20

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.

3
  • 3
    I agree that indirect buffers have benefits. But your first point doesn't need the buffers to be indirect. You can see and edit multiple locations of the same buffer even if the same buffer is simply opened in two windows. But yes, independent narrowing, folding, etc are the strong features of an indirect buffer in my opinion. Commented May 6, 2015 at 13:16
  • 7
    Also edit the same buffer with multiple modes, (combine indirect buffer with narrowing). You can edit some parts of a buffer with markdown modes, another part with python modes, another part with json modes
    – freakhill
    Commented May 6, 2015 at 13:33
  • You can see multiple locations using multiple windows alone, but since there is only one value of point in this situation, switching buffers won't preserve the locations in both windows, but one only.
    – politza
    Commented May 7, 2015 at 14:41
12

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.

6

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 narrow-indirect.el says:

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 g to 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.