The Stack Overflow podcast is back! Listen to an interview with our new CEO.
26

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


17

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.


8

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.


6

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


5

Test the variable this-command to see whether it is kill-buffer


5

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


5

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 () (interactive) (catch 'quit (save-window-excursion (let (done) (...


5

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


4

Or the simpler one would be : (setq kill-buffer-query-functions nil) Inspired from abo-abo config


4

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


3

(require 'cl) ;; for `copy-list' (let* ((frame-list (frame-list)) (current-frame (selected-frame)) (result (delq nil (mapcar (lambda (frame) (with-selected-frame frame (when (one-window-p) (with-current-buffer (car (buffer-list frame)) (if (...


3

You can iterate over the window-list of each frame in frame-list. Rather than: (delete (current-buffer) (seq-filter #'buffer-file-name (buffer-list))) You could use: (let (buflist) (dolist (frame (frame-list)) (dolist (window (window-list frame t)) (push (window-buffer window) buflist))) (seq-filter (lambda (buf) (...


3

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


3

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


3

I think the best way to handle this is to use cl-letf, like so: (require 'cl-lib) (cl-letf (((symbol-function 'yes-or-no-p) #'y-or-n-p)) (let* ((filename "~/private/tmp/foofile") (buf (get-buffer-create "*foo*"))) (with-current-buffer buf (write-file filename) (insert "Every good boy deserves fudge.")) (kill-buffer buf) (...


3

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


3

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: (recentf-mode) You can then put the defun you found in there, and bind it to a key,...


3

(defun my-delete-backward-word () (interactive "*") (push-mark) (backward-word) (delete-region (point) (mark)))


2

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


2

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


2

After googling around and tweaking the code snippets from the internet, I got this solution and it works as expected: (defvar all-gud-modes '(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. ...


2

Advice kill-buffer: (defun yes-or-no-p->-y-or-n-p (orig-fun &rest r) (cl-letf (((symbol-function 'yes-or-no-p) #'y-or-n-p)) (apply orig-fun r))) (advice-add 'kill-buffer :around #'yes-or-no-p->-y-or-n-p) or advice yes-or-no-p (defun yes-or-no-p@maybe-just-y-or-n-p (orig-fun prompt) (funcall (if (eq this-command 'kill-buffer) #'...


2

As suggested by Charkravarthy and Drew, using either vanilla Emacs, or together with Helm, Ido or Ivy one can kill / close the *Compilation* buffer using C-x k and selecting it when prompted to do so.


2

Another attempt at explaining the situation: (let ((buf "*Async Shell Command*")) (async-shell-command "cat") (while (let ((bp (get-buffer-process buf))) (message "status: %s" (process-status bp)) bp) (message "still has proc") (kill-process (get-buffer-process buf)) (accept-process-output) (message "Letting emacs ...


2

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


2

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


2

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: switch-to-visible-buffer ...


2

Since you don't want to kill all windows of a certain mode, or all of the buffers in a certain directory I recommend using ibuffer you can sort the list of windows by view time s v and then kill all the buffers you wish from the top of the list. n and p in rgrep open the files at pointer for viewing in a window, you can use arrow keys instead to just walk ...


2

Use a process sentinel when you want to react to a process exiting. e.g.: Refer to C-hig (elisp)Sentinels In this case shell only calls shell-mode when (and after) starting the inferior process, so we can use shell-mode-hook to add the sentinel. As @Tobias points out in the comments, set-process-sentinel will clobber any existing sentinel for that process....


1

Spacemacs caused this problem. spacemacs/useful-buffer-p filtered some asterisk buffers, so putting "" into the filtering regexp spacemacs-useless-buffers-regexp solved this problem.


Only top voted, non community-wiki answers of a minimum length are eligible