18

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

16

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.

| improve this answer | |
  • 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. – Kaushal Modi May 6 '15 at 13:16
  • 5
    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 May 6 '15 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 May 7 '15 at 14:41
10

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.

| improve this answer | |
4

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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