3

I have many buffers open in an Emacs window. I want to move one of the buffers to a new window. Is there a command which does that?

vineeshvs
  • 479
  • 7
  • 32

2 Answers2

9

The command you are looking for is tear-off-window. Note that this command must be associated with a mouse click event.

For example, you can put the following code (from this reddit comment) in your init file (more about init file here):

(global-set-key [mode-line C-mouse-1] 'tear-off-window)

This will call tear-off-window when you Control-click on the buffer modeline.

If you want to use a keyboard binding, the modified version of tear-off-window is below. Put it into your init file to save it after restarting emacs.

(bind-key "C-x C-X" #'my/tear-off-window)

(defun my/tear-off-window ()
  "Delete the selected window, and create a new frame displaying its buffer."
  (interactive)
  (let* ((window (selected-window))
     (buf (window-buffer window))
     (frame (make-frame)))          
    (select-frame frame)
    (switch-to-buffer buf)
    (delete-window window)))

In the code, the modified command is bind to "C-x C-X". You are free to change it to any other key sequence (more details here).

yantar92
  • 126
  • 3
  • Where do I keep the modified version of `tear-off-window`? I didn't understand how to make the command associate with mouse click event. Kindly elaborate. – vineeshvs Aug 02 '19 at 05:46
  • 1
    @yantar92 Now you will be able to vote on SO. And by the way, it was a very good answer. – pietrodito May 12 '20 at 07:20
  • Lets say I have 2 buffers open on a single frame, when I call your function, it opens a new frame with the current buffer, but it does not close the buffer on the first frame. Also, I do not know why but in the new frame, I can also see the second buffer which is not the active. When I call your function I want it to open a new frame and move the current buffer to the new frame, and kill the buffer on the previous frame. Is it possible? – efe373 Jul 13 '23 at 05:54
  • It is normal for Emacs to have a buffer displayed in multiple windows. If you want to kill all the buffer windows in a frame, you will need to loop over `window-list` in a frame and delete all the windows with buffer being the buffer of interest. – yantar92 Jul 13 '23 at 09:27
3

IIUC, you want to create a new WM window.

Emacs uses a slightly different terminology: what are usually called "windows" in a GUI environment, Emacs calls "frames". WIthin a single frame, Emacs subdivides its area into separate "windows" (IOW, even in a non-GUI environment, Emacs acts as a "tiling" window manager). So, you can create a new frame with C-x 5 2 (or equivalently M-x make-frame-command RET) and then in that frame, switch to the required buffer with C-x b <buffer-name> RET.

Note by the way, that you don't "move" the buffer to the new frame: the buffer exists independently of whether there is a (emacs) window in a frame that displays the buffer.

NickD
  • 5,937
  • 1
  • 21
  • 38
  • Both commands work. Also, noticed that if I close one such buffer using C-x C, it closes both all the frames. – vineeshvs Aug 02 '19 at 05:43
  • 1
    That's correct: as I said, the buffer exists independently of any windows that displays it. When it doesn't exist any longer, then no window can display it. BTW, you don't need separate frames to observe this: two windows in the same frame will behave the same way. – NickD Aug 02 '19 at 12:35