How can I get back the "natural" order in the Buffer List? - emacs

With C-x C-b the buffer list is displayed. First in it's natural order with most recently used buffers on top, and buried buffers at the bottom.
There, I can now sort the Buffer by name, size, mode and file. But once I click on such an option I cannot go back to the original ordering.
Also killing the buffer and recreating it does not change that order. (Using 25.2)
So how can I get that ordering back without restarting emacs?

There is another mode that's nowadays built-in to Emacs that can be used to display the buffer list: ibuffer-mode.
If you are not using it already, you can experiment with M-x ibuffer and find out its capabilities with C-h m. Note that in particular, it can sort the buffer list in various ways, one of which is by recency with s v, which is what the OP asked for; but note also that it has many other ways to sort that make it very flexible.
Once you are convinced that that's the way to go, you can redefine the C-x C-b keybinding in your init file with:
(global-set-key (kbd "C-x C-b") 'ibuffer)
I did that a long time ago and have never looked back. IMO, it should be the default: that may come to pass, but AFAIK that is still not the case.

Sorting order goes away as expected when I kill buffer named *Buffer List* and reopen using C-x C-b (Using emacs 27.1 & 28.1). From the EmacsWiki
Once sorted, there is no nice way to restore the default MRU sort. You
have to ‘kill-buffer’ the buffer menu buffer, and then re-open it.
(Sorting sets the variable ‘tabulated-list-sort-key’ in
tabulated-list.el. Its default is ‘nil’. No way to restore the nil
value is provided.)
May be worth adding a custom function which can disable the sort without closing the buffer.
(defun disable-buffer-sort()
(interactive)
(save-excursion
(set-buffer (get-buffer "*Buffer List*"))
(setq tabulated-list-sort-key 'nil)
(revert-buffer)
))
(disable-buffer-sort)
As #NickD mentioned in other answer ibuffer-mode is also a good alternative with more features.

Related

How to clear the Emacs buffer history?

When I press C-x b (ido-switch-buffer) I get a lot of buffers that I don't want to see. I'd like to clear the buffer history.
I tried evaluating this expression (using M-x eval-buffer):
(setq ido-buffer-history '())
And it took effect; I can tell because I looked at the variable with C-h v ido-buffer-history. However, the change did not get reflected in the minibuffer when I press C-x b.
What else should I do? Are there other variables I should be clearing?
UPDATE : The 'extra' buffers that I'm seeing are not active. Interestingly, C-x C-b (ido-fallback-command) shows exactly what I would expect. It is the buffer history that I'm interested in. (See the buffer-name-history and ido-buffer-history variables for more context.)
Note: Perhaps it will help to mention that I'm using the emacs-starter-kit which has ido-ubiquitous installed.
Add the following to your init.el: (setq ido-use-virtual-buffers nil)
For posterity:
Those are all the active buffers in your session. You can clean them with the following commands:
M-x clean-buffer-list will close any clean buffers you haven't used in a while
M-x kill-some-buffers will visit each buffer and ask if you want to close it
M-x kill-matching-buffers will prompt for a regex over buffer names, which you can just leave blank
Edit:
There's also the boring old buffer menu you get with C-x C-b. In this menu, you can hold d until it marks each buffer for deletion, then press x to commit.
Thanks to Chris, I learned about ido's virtual buffers. I don't want to disable ido-use-virtual-buffers altogether. I want to clear the history as needed; these commands accomplish that goal:
(setq ido-virtual-buffers '())
(setq recentf-list '())
(Note that clearing ido-virtual-buffers was not sufficient -- recentf-list also must be cleared.)
I found this entry on emacswiki.
The variable that stores the buffer history you are referring to is buffer-name-history
If you run M-x describe-variable RET buffer-name-history RET you will see all of the dead buffers that no longer really exist. The wiki recommends creating a hook that removes the buffer name from the list whenever you kill a buffer.
I just did: M-x eval-expression RET (setq buffer-name-history '()) RET
This seems to have worked. The next time I ran C-x b I only cycled-through my real buffers.
That said, setting the variable to nil seems to completely disable the functionality (the variable doesn't seem to be re-populated once I open more buffers).

how to stop Emacs from opening new buffers when following a link in customization

The problem occurs when customizing options in Emacs. Every time I click on a link a new buffer is created. How to force Emacs to use single buffer?
Try this:
(defadvice custom-buffer-create (before my-advice-custom-buffer-create)
"Exit the current Customize buffer before creating a new one, unless there are modified widgets."
(if (eq major-mode 'Custom-mode)
(let ((custom-buffer-done-kill t)
(custom-buffer-modified nil))
(mapc (lambda (widget)
(and (not custom-buffer-modified)
(eq (widget-get widget :custom-state) 'modified)
(setq custom-buffer-modified t)))
custom-options)
(if (not custom-buffer-modified)
(Custom-buffer-done)))))
(ad-activate 'custom-buffer-create)
As an alternative to my original answer (which I am not inclined to use myself), I thought I might suggest other ways in which you might deal with getting rid of lots of Customize buffers once you have finished customising things.
First, do note that simply pressing q will "Exit current Custom buffer according to `custom-buffer-done-kill'" (i.e. either bury it or kill it).
Second is to use M-x kill-matching-buffers RET \*Customize RET (and then confirm each one), but that's a bit tedious.
What I'd actually do is use ibuffer.
If you don't use it already, I recommend binding C-x C-b to ibuffer, which is a greatly enhanced alternative to the default list-buffers. I love it primarily for its filtering and grouping abilities, but it can do a great deal besides that.
(global-set-key (kbd "C-x C-b") 'ibuffer)
I also use the advice which can currently be found here at the Emacs Wiki so that ibuffer always opens with the buffer I came from selected.
That done, and from a Customize buffer, C-x C-b * M RET D y will kill all the Customize buffers. That is:
C-x C-b Open ibuffer (with point on the current Customize buffer entry)
* M Mark buffers by major mode...
RET ...select the mode (which defaulted to the major mode of the selected buffer, or otherwise type Custom-mode RET)
D y kill all marked buffers
Try it out; you'll probably like it. Searching for ibuffer will turn up other handy uses.
For instance, you could use / n ^\* RET / g tmp RET to separate out all buffers starting with * into a "tmp" group, so that they don't clutter up the group of buffers you are more likely to be interested in.
As with any major mode, use C-h m to read the built-in documentation.

How do you list and manage hidden buffers?

When calling switch-to-buffer, in the minibuffer, when you press SPACE, you can see hidden buffers that you normally don't see, like *Minibuf-0* for example.
How could you list those hidden buffers into the list of buffers shown by list-buffers ? If it's not possible using list-buffers, how do you manage them ?
You can tweak the function to show all buffers, like so:
(defun list-all-buffers (&optional files-only)
"Display a list of names of existing buffers.
The list is displayed in a buffer named `*Buffer List*'.
Non-null optional arg FILES-ONLY means mention only file buffers.
For more information, see the function `buffer-menu'."
(interactive "P")
(display-buffer (list-buffers-noselect files-only (buffer-list))))
(define-key ctl-x-map "\C-b" 'list-all-buffers)
ElectricBufferList does it for me:
(global-set-key "\C-x\C-b" 'electric-buffer-list)
Shows all buffers.
I prefer bs-show to list-buffers and electric-buffer-list.
bs-show can be configured to display all buffers by changing the value of the variable bs--intern-show-never
I think though that because they begin with a space, they're not supposed to be easily visible to you. They're more like internal variables, and manipulating them may make things start misbehaving pretty quickly. Best to just ignore them for the most part.
In any buffer do...
(buffer-list) C-x C-e
The list will now be in *messages*
now leave those hidden buffers alone ;-)

How to force Emacs to save even if it thinks (no changes need to be saved)

This happens to me all the time:
I have a file open in emacs,
I save it ('save-buffer),
the file changes on disk (or get's deleted, moved, etc.)
but I want it back, so I try to save again in emacs ('save-buffer) and instead of saving it says "(no changes need to be saved)" and does nothing.
Is there a different function, or a setting, that I can use to force emacs to save?
Wrap a function around save-buffer that marks the buffer modified first:
(defun save-buffer-always ()
"Save the buffer even if it is not modified."
(interactive)
(set-buffer-modified-p t)
(save-buffer))
You can save as, with C-x C-w. That should save unconditionally. You can also just type a space then backspace over it. Emacs is smart enough to realize that if you undo everything you've done so far the buffer has no changes, but if you make changes and then manually reverse them it will consider the buffer to have been changed.
You can mark the current buffer as modified using the Emacs-Lisp function not-modified with a prefix arg, bound to:
C-u M-~
The answer above won't work if you don't call the new function directly.
If you want to seamlessly change emacs saving behavior. The best solution is to create an advice:
(defadvice save-buffer (before save-buffer-always activate)
"always save buffer"
(set-buffer-modified-p t))
As a slight alternative to scottfrazer's answer:
(defun my-save-buffer-always-sometimes (prefix)
"Save the buffer even if it is not modified."
(interactive "P")
(when prefix
(set-buffer-modified-p t))
(save-buffer))
This was you can force it when you want to with a prefix (C-u C-x C-s) but not unnecessarily change a file otherwise. The last modified timestamp is very useful (e.g. source code control) that it seems a shame to change it arbitrarily. YMMV, of course.
A similar problem brought me online to look for a solution. Then it hits me that all I have to do is type a space (or any character) and delete it, which marks the buffer as changed. Then I can type C-x C-s as normal. Maybe not sophisticated or advanced, but it works.
Like Tagore Smith said, you can force Emacs to save the buffer with C-x C-w.
If you're using Evil mode, you can also achieve this behavior by typing :w! in normal state. Unlike C-x C-w, :w! will not prompt you for the filename to save to.

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.