How to move an emacs buffer to a new window? - emacs

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?

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

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.

Related

Emacs: How do I create a new "empty" buffer whenever creating a new frame?

My Emacs is on OS X system. Is there any way to make a new frame defaulted to an empty buffer whenever I use ⌘N (just like the way TextEdit works)? I prefer to write contents first and decide an appropriate filename later. However, Emacs wants me to decide the filename first and write contents later. I don't see any advantage for it. Does anyone know why Emacs works that way?
Basically, if I use C-x 5 2, Emacs always pops up a frame with whatever file I am currently working on. This is inconvenient. I also don't want my Emacs to pop up a new frame defaulted to *scratch* (many Google search results somehow suggest this approach). I prefer it to have a buffer temporarily called "Untitled" in the new frame, and if I use ⌘N again, Emacs pops up another temporarily "Untitled 2" buffer, and so on. In this way, I can decide the buffer filenames later.
You can create new buffers with switch-to-buffer. Type C-x b, enter a buffer name, and press RET. If no buffer with that name exists, Emacs creates a new one automatically in Fundamental Mode. You may switch to any other mode as usual with M-x, e.g. M-x python-mode. To change the default buffer, set the default value of major-mode to the desired buffer.
If you'd like to have a buffer name chosen automatically, and create a new frame, however, you need to write your own command:
(defun lunaryorn-new-buffer-frame ()
"Create a new frame with a new empty buffer."
(interactive)
(let ((buffer (generate-new-buffer "untitled")))
(set-buffer-major-mode buffer)
(display-buffer buffer '(display-buffer-pop-up-frame . nil))))
Bind this to C-c n:
(global-set-key (kbd "C-c n") #'lunaryorn-new-buffer-frame)
Now pressing C-c n creates a new frame with a new empty buffer named “untitled” where x is a consecutive number.
The following will create a buffer with a unique name. The buffer is not associated with any file, so if/when you ever C-x C-s save-buffer, you will be prompted to supply a filename.
(defun empty-frame ()
"Open a new frame with a buffer named Untitled<N>.
The buffer is not associated with a file."
(interactive)
(switch-to-buffer-other-frame (generate-new-buffer "Untitled")))
This might work for you, if I understand your request:
(defun empty-frame ()
(interactive)
(let ((fl (make-temp-file "Untitled")))
(switch-to-buffer-other-frame fl)))
This will open a new temporary file for each new buffer. If you'd rather not actually create the file, you can use this instead:
(defun empty-frame ()
(interactive)
(let ((bn "Untitled-")
(num 1))
(while
(get-buffer (concat bn (number-to-string num)))
(setq num (1+ num)))
(switch-to-buffer-other-frame
(concat bn (number-to-string num)))))
You've seen answers as to how to create new "Untitled" buffers, but as for why Emacs wants you to first choose a name, some of the reasons are:
Historical: that's just how it worked, and once you get used to it, it's no worse than the alternative.
Major modes and various other details are usually chosen based on the name of the file. So instead of creating an Untitled buffer and having to choose whether to put it into LaTeX mode or C mode, you just open a file with extension ".tex" or ".c".
Having a file name means that Emacs can use the standard auto-save procedure, whereas with the Untitled approach, applications need to have some special way to auto-save those Untitled documents at some "standard" place.
C-x b *untitled* will open new buffer if not exist. Also, see Emacs manual

Opening a window into a specified buffer

How can I open a new window (for example using C-x 3) into a new buffer, rather than a mirrored buffer that just echoes what I type.
So for example, let's say I'm messing around with python and I want to run the script in the shell. As it is currently I do this: C-x 3, M-x shell and then start it up and running. I'd rather just C-x 3 and it automatically opens into shell. I'm really new to Emacs so I don't know where to look for this.
It sounds to me like this, or something similar, is what you are looking for:
(defun pop-to-buff-at-right (buffer)
"Pop to BUFFER at the right of the current window."
(interactive "B")
(pop-to-buffer buffer '(display-buffer-in-side-window
(side . right)
(inhibit-same-window . t))))
You do not want to just split the window, which is specifically about showing the same buffer twice. You want to switch to another buffer, but you want it to be displayed to the right of the current window.
In emacs it is easy to define custom commands and bind it to keys. For instance, if you add this to your init file:
(defun open-shell-at-left ()
(interactive) ;; Tell emacs this function can be called interactively
(split-window-right) ;; Just what C-x 3 does
(shell)) ;; Just what M-x shell does
(global-set-key (kbd "C-c 3") 'open-shell-at-left)
You will have what you want when you type C-c 3. In general, you can find documentation about what a key binding does by typing C-h k and the keybinding. From that point, it is easy to chain existing commands into new ones.

Emacs ido start with open buffers first in cycle list

When switching buffers with emacs ido mode enabled, a list of completions are displayed in the minibuffer. It appears there is a "feature" that buffers that are already open are put to the end of the list. I, however, often open the same buffer in multiple panes.
Is there a way to either turn this "feature" off, or alternatively do the opposite: have the buffers that are already open be at the front of the completion list?
The main point of ido mode is that you don't use arrows to navigate between buffers in the minibuffer. Instead you type the part of the buffer's name. In this case it doesn't matter where the buffer is in the list.
This is not possible unless you want to wade deep in ido's intestines.
As eGlyph already said: You're likely using ido wrongly (and there's also C-s for <right> and C-r for <left>; no need for arrow keys).
But you can define command for choosing among the already shown buffers (here only from the current frame, if you want all shown buffers you have to collect the windows first via `frame-list):
(defun choose-from-shown-buffers ()
(interactive)
(let ((buffers (mapcar (lambda (window)
(buffer-name (window-buffer window)))
(window-list))))
(pop-to-buffer (ido-completing-read "Buffer: " buffers))))

Dedicated window for dired mode in Emacs?

I have emacs behaving more or less how I want it to by using this common bit of elisp:
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
Unfortunately, dired uses the directory for the buffer name, so dedicating a dired window only dedicates it to that directory. Once you navigate up or down, it opens a new buffer in a separate window. What I would like to do is dedicate a window to a major mode (dired in this case), and have all new buffers that default to that mode prefer that window. Is this possible?
Try using your code in combination with dired-single, which will cause all dired navigation to happen within a single buffer named *dired*. In the interests of full disclosure, I wrote dired-single.
set-window-dedicated-p forces Emacs to only show that window for that buffer, the other dired buffers cannot use the same window. See the *info* page for set-window-dedicated-p:
`display-buffer' (*note Choosing
Window::) never uses a dedicated
window for displaying another buffer
in it.
Perhaps one of the packages on the wiki page for DiredReuseDirectoryBuffer provides the functionality you're looking for...

emacs buffer bind to key

I don't know why but currently emacs opens only one copy of w3m. If w3m is already open then retyping the command to open w3m takes me to the already opened buffer. I would like to configure ansi-term similarly i.e. typing C-x C-a (command open ansi-term) should take me to already opened ansi-term instead of opening a new buffer altogether.
How can I achieve this in emacs?
You could write a wrapper function around ansi-term that checks to see if there already is an existing terminal buffer, and recycles that buffer if it exists:
(defun green-ansi-term ()
"Show an existing buffer called \"*ansi-term*\" if one exists, otherwise
call function ansi-term interactively."
(interactive)
(let ((existing-buffer (get-buffer "*ansi-term*")))
(if existing-buffer
(switch-to-buffer existing-buffer)
(call-interactively 'ansi-term))))