Emacs - Open buffer list without replacing another buffer - emacs

I have one frame, one window.
I use Cx 3, I now have two windows.
I use Cx Cb in order to see the list of buffers, however it opens it in another window but doesn't put the focus on it. It is even more annoying if I had opened a buffer on the 2nd window.
I would prefer to either open the buffer list in the window which currently has the focus, or temporarily change the focus to the buffer list.

First of all I want to start by saying that the ibuffer function does similary to what you want and does so in the current window, its definitely worth checking out.
Now onto your actual question. C-x C-b by default calls the function list-buffers. If you search for that command using C-h f it will show you the documentation, from there you can view the source code for the function by clicking on the underlined text that says buff-menu.el.
Now we can view the source of the list-buffers, the first function called is display-buffer. That sounds promising. We can now use C-h f once again to search for the display-buffer command. Reading though this documentation we see that the variable display-buffer-alist dictates how the display-buffer and in turn how list-buffers works. We can then see that by adding ("*Buffer List*" . display-buffer-same-window) to display-buffer-alist you will get the desired result.
All in all you simply need to put (add-to-list 'display-buffer-alist '("*Buffer List*" . display-buffer-same-window)) in your init file for your changes to take place.

Please just try one of these:
open the buffer list who currently has the focus:
(defun my:list-buffers (&optional arg)
(interactive "P")
(display-buffer (list-buffers-no-select arg) '(display-buffer-same-window)))
change the focus
(defun my:list-buffers2 (&optional arg)
(interactive "P")
(select-window (list-buffers arg)))

Related

emacs dispatch help window from original buffer

Whenever I do apropos, describe-key, or some other help function in emacs, it displays the help in my other window. In order to get rid of it, I must change windows/buffers to go there, type "q", and then change back to my original working buffer.
Is there a way I can do this in code somehow? I know how to save-excursion, switch buffers, etc, but I don't know how to send the "q" to the minibuffer/emacs while I'm over in the other buffer. Thanks
The help-window-select variable might be exactly what you want.
If you set its value to true (setq help-window-select t) then the help window will automatically be selected when you open it via one of the help commands. You can then press q to quit out of it and go back to your original buffer. There are many other options so you should check those out too.
For the apropos window or any window that uses display-buffer you can use.
(add-to-list 'display-buffer-alist
'("*Apropos*" display-buffer-same-window))
display-buffer-same-window is one options of many; it opens the buffer in the current window. The other possible options can be seen by looking up the docs on the display-buffer function.
Here's my solution to this problem. I bind this command to C-c q:
(defvar my/help-window-names
'(
;; Ubiquitous help buffers
"*Help*"
"*Apropos*"
"*Messages*"
"*Completions*"
;; Other general buffers
"*Command History*"
"*Compile-Log*"
"*disabled command*")
"Names of buffers that `my/quit-help-windows' should quit.")
(defun my/quit-help-windows (&optional kill frame)
"Quit all windows with help-like buffers.
Call `quit-windows-on' for every buffer named in
`my/help-windows-name'. The optional parameters KILL and FRAME
are just as in `quit-windows-on', except FRAME defaults to t (so
that only windows on the selected frame are considered).
Note that a nil value for FRAME cannot be distinguished from an
omitted parameter and will be ignored; use some other value if
you want to quit windows on all frames."
(interactive)
(let ((frame (or frame t)))
(dolist (name my/help-window-names)
(ignore-errors
(quit-windows-on name kill frame)))))
I suggest putting (winner-mode 1) in your init file, and then using C-c<left> to call winner-undo (repeatedly, if necessary) to return to a previous window configuration.

How to control which window to be used for new contents in Emacs?

Often I have multiples windows carefully arranged within a frame.
However, certain command, say M-x man RET will grab one of the visible windows to display its own content. Sometimes this is annoying because the window that was taken away is one that I need to keep it visible.
e.g. I have 3 windows on screen, one useful source-code window and two useless windows. I want to keep the soure-code window visible while checking the man page. But very often Emacs just take away the code-window for the newly opened man page.
One way I can think of is to display the (chronological) open order of each window, so that I can focus point on n-th window and be confident that Emacs will grab (n+1)-th window for new content.
Is there a way to display such order, e.g. in the mode-line of each window?
Or is there another way for better control for displaying new window?
A little late to the party but as discussed in the comments, using dedicated windows is a good way to control where new content is displayed (+1 to #lawlist for bringing it up and to #phils for mentioning toggling!).
I'm pretty sure you'd be able to implement a command that toggles dedicatedness yourself at this point, but since I have the code for this handy I'll share it anyway:
(defun toggle-window-dedicated ()
"Control whether or not Emacs is allowed to display another
buffer in current window."
(interactive)
(message
(if (let (window (get-buffer-window (current-buffer)))
; set-window-dedicated-p returns FLAG that was passed as
; second argument, thus can be used as COND for if:
(set-window-dedicated-p window (not (window-dedicated-p window))))
"%s: Can't touch this!"
"%s is up for grabs.")
(current-buffer)))
(global-set-key (kbd "C-c d") 'toggle-window-dedicated)
Now, in a multi-window setup you can simply press C-c d in each window you would like to "protect".
I'll throw this example usage of display-buffer-alist into the mix, given that it was mentioned in the comments, and is indeed a general mechanism for controlling how and where buffers are displayed (even though, as Drew indicates, it is far from trivial).
Start with the help for the variable itself:
C-hv display-buffer-alist RET
From there you'll get to the help for display-buffer, and no doubt acquire some idea of the complexities involved :)
In any case, the following is an example of using a custom function to decide how to display buffers named *Buffer List*.
You can easily see that if you can write a function which can figure out how to display a buffer where you want it to, then you can use display-buffer-alist to make it happen.
(defun my-display-buffer-pop-up-same-width-window (buffer alist)
"A `display-buffer' ACTION forcing a vertical window split.
See `split-window-sensibly' and `display-buffer-pop-up-window'."
(let ((split-width-threshold nil)
(split-height-threshold 0))
(display-buffer-pop-up-window buffer alist)))
(add-to-list 'display-buffer-alist
'("\\*Buffer List\\*" my-display-buffer-pop-up-same-width-window))
A key quote regarding the ACTION functions is:
Each such FUNCTION should accept two arguments: the buffer to
display and an alist. Based on those arguments, it should
display the buffer and return the window.

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 list-buffers behavior

In GNU emacs, every time I hit Ctrl-x Ctrl-b to see all of my buffers, the window is split to show the buffer list, or if I have my window already split in 2 (for instance, I will have a shell running in the lower window), the buffer list appears in the other window.
My desired behavior is for the buffer list to appear in my active window so that I can select the buffer I want and continue to working in the same window, rather than having to Ctrl-x Ctrl-o to the other buffer, selecting the buffer (with enter) and editing that buffer in the other window... I've googled for it but it doesn't seem to be a common desire? I wonder if anyone has an elispy (or other) solution?
You might want to rebind C-x C-b to invoke buffer-menu rather than list-buffers:
(global-set-key "\C-x\C-b" 'buffer-menu)
Just customize the variable same-window-regexps. display-buffer will display any buffer whose name matches a regexp there in the currently-selected window.
(You will want to add "[*]Buffer List".)
not exactly a solution, but ido-mode provides a different and powerful way to interact with buffers. C-x b will then show a list of all the open buffers, and the one you select will open in the current window.
Strangely, there isn't an answer here about ibuffer.
I would recommend this as a standard change for the majority of Emacs users:
(global-set-key (kbd "C-x C-b") 'ibuffer)
ibuffer is a very advanced replacement for the default buffer listing, and not only features the exact behaviour requested, but provides a wealth of other functionality.
I listed a few ibuffer filtering and grouping basics in
Emacs: help me understand file/buffer management, but be sure to read the documentation for details.
Try to add
(ido-mode 1)
to your .emacs, and enjoy the result :)
If you like the original buffer list (as opposed to the 'buffer-menu solution proposed by others), you can use this:
(global-set-key (kbd "C-x C-b") 'my-list-buffers)
(defun my-list-buffers (&optional files-only)
"Display a list of names of existing buffers.
The list is displayed in a buffer named `*Buffer List*'.
Note that buffers with names starting with spaces are omitted.
Non-null optional arg FILES-ONLY means mention only file buffers.
For more information, see the function `buffer-menu'."
(interactive "P")
(switch-to-buffer (list-buffers-noselect files-only)))
Which is the same function as before, only in the current window.
I highly recommend bs.el from http://www.geekware.de/software/emacs/ Install it and:
(require 'bs)
(add-hook 'bs-mode-hook 'turn-on-font-lock)
(global-set-key "\C-x\C-b" 'bs-show)
It manages buffers and window configuration in the right way, so everything requires minimum number of keystrokes.
Not sure where I got this but:
;;; Faster buffer switching
(global-set-key [(C tab)] 'buffer-menu)
This makes Ctrl-Tab display all buffers in the current window. You can then navigate to a buffer and hit Enter to visit it.
Another not-what-you-asked-for solution: don't select the desired buffer with the mouse, rather finish typing its name (use tab-completion to reduce keystrokes and increase accuracy), then hit return. The buffer list will disappear, and the new file will be open in the previously active window.

How do I close an automatically opened window in Emacs?

This is probably a very naive Emacs question - I'm new to it.
When I'm evaluating a lisp expression, if there's an error the debugger automatically comes up in another window. If I have *scratch* and *info* open (the former for trying out lisp and the latter for reading about it), then the debugger opens up in the window that *info* was in. At the moment, I have to switch to that window, then change it back to *info*, before returning to *scratch*. (The same thing happens if I do C-x C-b for a list of buffers.) I'm guessing there has to be a way to just close that window without this long sequence of commands. Can anyone enlighten me?
At least here on my emacs (22.3), when the debugger pops up, its window becomes the active one. There, pressing q just quits the debugger, if that's what you want. At that point, it also gets out of recursive editing.
From what I understand, you want to close the buffer in the other window without moving your cursor from the current window.
I don't any existing function does that, so I rolled my own.
(defun other-window-kill-buffer ()
"Kill the buffer in the other window"
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(select-window win-curr)))
You can bind it to something like "C-x K" or some other somewhat difficult-to-press key so you won't press it by mistake.
(global-set-key (kbd "C-x K") 'other-window-kill-buffer)
I use this a LOT! (for Help buffers, Compilation buffers, Grep buffers, and just plain old buffers I want to close now, without moving the point)
I'm usually using the delete-other-windows command. C-x 1.
It's so regullar, that I rebinded to F4.
Official docs: https://www.gnu.org/software/emacs/manual/html_node/emacs/Change-Window.html#Change-Window
HTH
Using winner mode, you can use the keybinding C-C left arrow to return to the previous window configuration. Of course, this doesn't actually kill the new buffer, but it does hide it.
I think you're looking for C-x 4 C-o, which displays a buffer in the "other" window without switching to it.
As mentioned above, in the case of the backtrace buffer you probably want to exit from it with q, to get out of the recursive edit.
Try to use popwin-mode. It is really good about closing windows automatically.
I want to describe how mechanism work|
You open undo-tree-visualize than you find your correct branch, after that when you change your active window which has cursor via switch-window undo-tree related buffer will close other window automatically.
One possible solution is to use a window management package. Store your "preferred" window arrangement, and after the debugger/buffer window pops up and you're done with it, revert to your preferred arrangement.
There are a bunch of packages to choose from, see: switching window configurations on the wiki.
Additionally, you might want to figure out the actions you commonly do that trigger the extra (unwanted) window popping up, and use that to trigger saving your window configuration just before the window pops up.
If you want to roll your own (it's pretty easy), you can just save off the window configuration, and restore it like so:
(setq the-window-configuration-i-want (current-window-configuration))
(global-set-key (kbd "<f7>")
(lambda () (interactive)
(set-window-configuration the-window-configuration-i-want)))
The trick is figuring out where to put the setting of the-window-configuration-i-want.
Thanks go to #spk for a nice function. I merely modified it to also close the window too and bound it to the already nice set of window control functions found in evil-windows (via "C-w"):
(use-package evil
:bind (:map evil-window-map ("O" . delete-most-recent-window))
:preface
(defun delete-most-recent-window ()
"Kill the buffer in the most recent window."
(interactive)
;; Window selection is used because point goes to a different window
;; if more than 2 windows are present
(let ((win-curr (selected-window))
(win-other (next-window)))
(select-window win-other)
(kill-this-buffer)
(delete-window) ; <-- Added this
(select-window win-curr))))