30
August
2008

Emacs: kill-matching-buffers-by1

Nothing fancy here, just some emacs functions that assist in killing multiple buffers based on buffer name or file name. I wrote this to help me cope with the large number of log files I so frequently open in emacs (where else?).

(defun act-on-buffers (pred action)
  (let ((count 0))
	(dolist(buffer (buffer-list))
	  (when (funcall pred buffer)
		(setq count (1+ count))
		(funcall action buffer)))
	count))
 
(defun kill-matching-buffers-by (acc)
  "Kill buffers whose name matches the input"
  (let* 
	  ((re (read-string (format "kill buffers matching: ")))
	   (match (function 
			   (lambda (buf) 
				 (string-match re (funcall acc buf)))))
	   (kill #'(lambda (buf) (kill-buffer buf)))
	   (count (act-on-buffers match kill)))
 
    (message "%d buffer(s) killed" count)))
 
(defun kill-matching-buffers-by-name ()
  (interactive)
  (kill-matching-buffers-by 'buffer-name))
 
 
(defun kill-matching-buffers-by-filename ()
  (interactive)
  (kill-matching-buffers-by #'(lambda (b) (or (buffer-file-name b) ""))))


1 comment

  1. James:

    Hi, I found your blog on this new directory of WordPress Blogs at blackhatbootcamp.com/listofwordpressblogs. I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.



Leave a Reply