vineeshvs
vineeshvs

Reputation: 565

How to move an emacs buffer to a new window?

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?

Upvotes: 3

Views: 3764

Answers (2)

yantar92
yantar92

Reputation: 136

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).

Upvotes: 10

NickD
NickD

Reputation: 6422

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.

Upvotes: 3

Related Questions