Here's another, simple alternative that doesn't require recentf. Hooking the first function into kill-buffer-hook will push the filename associated with the buffer onto a list. (Note that, if you kill a buffer that's not visiting a file, it's gone for good.) The latter function pops that file off of the list and visits it:
(defvar killed-file-list nil
Remove the corresponding function from the relevant variable :
(setq kill-buffer-query-functions (delq 'process-kill-buffer-query-function kill-buffer-query-functions))
How did I find this ?
kill-this-buffer uses kill-buffer internally, which references the variable kill-buffer-query-functions.
What you want is built-in to emacs. Put (require 'ibuffer) in your config file, then bind a key to 'ibuffer:
(global-set-key "\C-x\C-b" 'ibuffer)
Be sure to check out its help documentation. It has a lot of features.
I would like to ask you: “Do you really want to kill it?”. Indeed, killing a
buffer is such a common thing in Emacs world, but once killed, buffer is
gone, and as your question demonstrates, it's not always desirable.
However we can choose another way, so that you never need to restore killed
buffer — just prefer burying to killing. Take a look at
Kill or ...
Based on the other answers and comments, here's a custom kill command that provides the option to diff a modified buffer with the file it is visiting. I've mapped this to C-x C-k but could also remap kill-buffer as suggested in @Drew's answer.
(defun my/kill-this-buffer ()
The big question is whether you want this behavior for kill-buffer itself, which means not only when you call it interactively but for every use of it in existing Lisp code, or whether you want it only for interactive use.
I'll assume the latter. In that case, leave kill-buffer alone, define your own command that does what you want, and remap the keys that ...
Use the function kill-this-buffer. I basically never want to kill buffers that are not the current buffer, so I rebind C-x k to that function:
(global-set-key (kbd "C-x k") #'kill-this-buffer)
(Of course, if you ever do wanted to be prompted for a buffer to kill, you can just run M-x kill-buffer, or since you use Helm, kill it from the buffer switcher.)
I use this solution from this SO post and it works fine.
The solution is elegant but not perfect; it stores a list of active buffers and returns the first file from the the recentf-list that doesn't belong to the list of active buffers.
;; Reopen the last killed buffer
;; Source: https://stackoverflow.com/questions/10394213/emacs-reopen-previous-killed-...
You can use M-x ibuffer which I recommend binding to C-xC-b to replace the default (or whatever binding you prefer).
Use %m to mark buffers by matching a regexp against the displayed mode-name (e.g. Emacs-Lisp). Then use D to kill the marked buffers.
"notmuch-" or "[Notmuch]"
Note that, as we're dealing with regexps, you would need to ...
You can iterate over the window-list of each frame in frame-list.
(seq-filter #'buffer-file-name (buffer-list)))
You could use:
(dolist (frame (frame-list))
(dolist (window (window-list frame t))
(push (window-buffer window) buflist)))
(seq-filter (lambda (buf)
For me, using emacsclient with the '-n' switch prevents me from getting the "no client connection" warning.
Invoking emacsclient file from the command line will open file in the current Emacs session. The command line from which you ran emacsclient will be waiting for the file to be closed by the server, and then terminate. You won't be able to execute ...
There are multiple ways to specify the key you want to bind. [C-u] doesn't work; if you're supplying a vector, you have to use it a little differently. From that link:
In the vector representation, each element of the vector represents an
input event, in its Lisp form. See Input Events. For example, the
vector [?\C-x ?l] represents the key sequence C-...
I think the best way to handle this is to use cl-letf, like so:
(cl-letf (((symbol-function 'yes-or-no-p) #'y-or-n-p))
(let* ((filename "~/private/tmp/foofile")
(buf (get-buffer-create "*foo*")))
(insert "Every good boy deserves fudge."))
just in case I decide to exit out of the function with a 'quit signal such as C-g, in which case the effect would become long-lasting rather than merely temporary.
This can be solved with unwind-protect. The following function always restores yes-or-no-p's definition at the end even if you quit in the middle of it:
(defun my/kill-buffer ()
You need to turn on recentf-mode. To do that, run M-x recentf-mode. Then, the function might not work until you open or kill some new buffers; I don't think you'll have recentf-list filled out.
If you want this to be enabled when Emacs starts up, put this in your init file:
You can then put the defun you found in there, and bind it to a key,...
This answer takes the other answers and the comments into account, offering better ways of doing the same things. Your original question was about deleting the diff buffer after committing, though the other answers concentrated on doing something when quitting the status buffer. So I will do that first too, but later also tell you how to do something when ...
You can do this with Helm
by entering a partial major-mode name prefixed with *. For example: *lisp, *sh etc. You can also use negation, e.g. !*org
to narrow down all non-Org mode buffers, or specify multiple
major modes, e.g. *!lisp,!sh,!fun etc.
Then press M-a to mark those buffers and M-D to kill them.
For example, to kill all dired buffers:
I made it slightly different (but the previous answers helped me a lot, thanks guys), to avoid string matching.
I modified the "new-empty-buffer" function suggested by Xah Lee from ErgoEmacs, making it set a local-variable 'custom-scratch-buffer when the buffer is created.
Therefore, we know when the buffer is a scratch buffer created by this function and ...
I guess much of this is a matter of taste and usage patterns. I tend to use something inbetween your solution and @Drew's usage (according to the comments).
Here is the gist of it:
I want "throwaway" buffers to be quickly available, without having to think ahead of time where to save them;
I want them to be file buffers in order for Emacs to warn me if I ...
EDIT: I didn't pay attention when answering, and answered something else the OP didn't ask. Once again, I'm sorry. Thank you for
your words, @CodyChan.
Well, I'm no Emacs veteran, and maybe this possibly has became available just on recent versions. I know I'm coming some years later, but maybe it can be helpful for others, since my search got me here.
After googling around and tweaking the code snippets from the internet, I got this solution and it works as expected:
'(gud-mode comint-mode gdb-locals-mode gdb-frames-mode gdb-breakpoints-mode)
"A list of modes when using gdb")
(defun kill-all-gud-buffers ()
"Kill all gud buffers including Debugger, Locals, Frames, Breakpoints.
As mentioned, Ibuffer (ibuffer) and Buffer Menu (buffer-menu) are the closest to a Dired-like interface. But you can also list and act on multiple buffers using just completion.
With Icicles, commands that show you buffer names for completion let you use S-<delete> on any number of them to kill those buffer.
Similarly, C-x k (icicle-kill-buffer, in ...
I had the same issue with the newer Emacsen. I finally found a solution that seems to work. There is a configuration setting called switch-to-visible-buffer which default to t. Set it to nil via M-x customize-variable or with a
(set switch-visible-buffer nil) in your $HOME/.emacs file.
Here's the help for switch-to-visible-buffer: