Splitting and Merging Frames
The following two functions are useful to transfer parts of the window layout (see Basic Concepts of Emacs Windows) of one frame to two separate frames and to merge the window layouts of two separate frames into one frame.
Command: split-frame &optional frame arg
This command transfers some windows of the specified frame to a new frame and subsequently deletes them on frame. frame must be a live frame and defaults to the selected frame. arg specifies the number of windows to transfer and defaults to 1. Interactively, arg is the prefix argument.
In a first step, it divides the child windows (see Windows and Frames) of frame’s main window (see Side Windows) into two parts. The first part includes the first arg child windows if arg is positive, and the −arg last child windows if arg is negative. The second part includes the remaining child windows of frame’s main window.
In a second step, it clones (see Window Configurations) into a newly created frame each of the windows of the part which does not include frame’s selected window (see Selecting Windows) and removes those windows from frame. Note that the original identity (see Basic Concepts of Emacs Windows) of the cloned windows is lost in this step. The identity of the windows retained on frame is preserved, however. In a final step, it deletes the windows that have been cloned from frame and returns the newly created frame.
This function signals an error if arg is either zero or not a number, or if frame’s main window is either live or does not have more child windows than specified by the absolute value of arg.
Command: merge-frames &optional frame1 frame2 vertical
This command transfers the main window (see Side Windows) of frame2 to frame1. Both frame1 and frame2 must specify live frames.
In the first step it splits the main window of frame1. If vertical is non-nil, it makes the new window below the old main window of frame1. Otherwise, it makes the new window on the right of frame1’s main window.
In the second step it makes the new window a clone (see Window Configurations) of the main window of frame2. The original identity (see Basic Concepts of Emacs Windows) of the cloned windows is lost in that step. In the final step, it deletes frame2 if the merge completed successfully and returns frame1.
Interactively, vertical is the prefix argument, frame1 is the selected frame and frame2 is the frame following frame1 in the list of all frames (see Finding All Frames). If frame1 and frame2 are omitted or nil in calls from Lisp, they default to the above values.
In general you cannot “undo” a split-frame call with a subsequent merge-frames call for the following reasons: In its second step merge-frame puts the main window of frame2 into an internal window split off in the first step. No such window was present in the configuration of frame1 before calling split-frame. Also, after merging is complete, the window cloned from the main window of frame2 will occupy as much space as the main window of frame1 before merging. This might not match the space distribution of the original layout.
Hence, if your preferred workflow is to temporarily split off windows for working on them in a separate frame and later continue with the original layout of the original frame, you should proceed as follows: Save the configuration (see Window Configurations) of the original frame, call split-frame to create the separate frame and finally restore the saved configuration of the original frame and delete the separate frame.