Edit one file in two different buffers in emacs

6,955

Solution 1

You can open the same buffer in more than one window (which can be spread amongst different frames), but it's not very convenient. Each window has its own point, but they all share the mark, the file mode, the narrowing and other characteristics, because apart from the point pretty much all characteristics are tied with the buffer. Also, if you happen to visit another buffer in one window, you'll lose your place in the file.

You can make an indirect buffer which has its own point, mark, modes and so on, but the same content as the original buffer (and saving either buffer writes to the same file). To create a second buffer that is a clone of the current buffer, run M-x clone-indirect-buffer RET. To open that second buffer in a different window, you can type C-x 4 c.

Solution 2

Split screen mode: where ctrl+x means press and hold ctrl key and type an x. Then type the number following.

ctrl+x 2 (horizontal split)

or

ctrl+x 3 (vertical split)

Then you can scroll them independently on the same file or open another buffer if you wish in one of them.

To return to a single view type

ctrl+x 1

If you wish you can split each screen (section) as many times as required. Select the screen you want to split first and then perform a horizontal or vertical split on it.

Solution 3

Emacs is biased to panes (windows), not frames. It is often desirable to open the same buffer in another frame, not just another window in the same frame. But C-x 5 c is unbound by default. This code define the missing clone-indirect-buffer-other-frame function:

(global-set-key [?\C-x ?5 ?c]
             '(lambda(newname display-flag)
               "Like `clone-indirect-buffer-other-window' but display in another frame."
               (interactive
                (progn
                  (if (get major-mode 'no-clone-indirect)
                      (error "Cannot indirectly clone a buffer in %s mode" mode-name))
                  (list (if current-prefix-arg
                            (read-buffer "Name of indirect buffer: " (current-buffer))) t)))
               (save-window-excursion
                 (let ((newbuf (clone-indirect-buffer newname display-flag)))
                   (switch-to-buffer-other-frame newbuf)
                   )
                 )
               )
            )
Share:
6,955
Dror
Author by

Dror

Updated on September 18, 2022

Comments

  • Dror
    Dror almost 2 years

    I would like to open the file foo.bar twice (or more) in emacs, so I can edit two different parts of it simultaneously. Is it possible? Probably the better question is, how to do it? Is there a way to open each instance in its own buffer/frame?

    • MMT
      MMT almost 13 years
      You can display the same buffer in different windows. Isn't it a solution for this problem?
  • dmckee --- ex-moderator kitten
    dmckee --- ex-moderator kitten almost 13 years
    Yikes! Shouldn't you include keyboard navigation between frames? C-x o runs other-window and cycles through the visible "windows" in each "frame". (Using the emacs configuration where window manager windows are called frames, and the separate panels inside them are called windows (I know, I know, but I'm not responsible for it).)
  • hookenz
    hookenz almost 13 years
    I tend to use the mouse.
  • SabreWolfy
    SabreWolfy over 11 years
    To clarify: C-x 4 c runs clone-indirect-buffer-other-window, so this would be used instead of clone-indirect-buffer, not following that command. However, on my system, both of these commands appear to do the same thing.
  • pedz
    pedz about 7 years
    I wish I could figure out how to remember this :-/
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' about 7 years
    @pedz C-x 4 is the prefix for windows, c is for clone. You may define other bindings if you prefer. C-x c is not used in the default configuration, you could use that if you aren't afraid of accidentally hitting C-x C-c instead. I define C-x 5 c to clone in a new frame in my init file.