Kill buffers in consult-buffer

2024-07-08

I recently switched to the great packages vertico and consult after using ivy+swiper for years. The main reason was that I wanted a leaner and faster approach. And consult is getting a lot more attention lately, while ivy seems to be standing still. I didn't integrate embark because I wanted to learn the basics first and see how far I could get there.

While using counsel to switch buffers, I was accustomed to hitting C-k inside the minibuffer completion and killing the current candidate from the completion list. This was not built into consult-buffer which is the replacement.

This little snippet provides the ability to kill a buffer in the minibuffer completion.

1
2
3
4
5
6
7
  (defun consult-switch-buffer-kill ()
    "Kill candidate buffer at point within the minibuffer completion."
    (interactive)
    ; The vertico--candidate has a irregular char at the end.
    (let ((name  (substring (vertico--candidate) 0 -1)))
      (when (bufferp (get-buffer name))
        (kill-buffer name))))

This function is bound to C-k in my minibuffer-local-map.

Feel free to send improvements or offer a way to do it independent of the completion framework.