web developer & system programmer

coder . cl

ramblings and thoughts on programming...


killing equally named buffers in emacs

published: 03-05-2011 / updated: 03-05-2011
posted in: development, emacs, programming, tips
by Daniel Molina Wegener

As you know I am an Emacs user. Also, I usually implement small commands on it to help me in my programming tasks. For example while I am working with Django — the Python framework — usually I must open various files with the same name, for example views.py and urls.py. Once you have modified various of those files and you want to close them, you need to visit each buffer and apply the kill-buffer command on each one. I have created — for my comfort — a pair of commands that can close various buffers at once.

The first one is batch-kill-buffer-equal-named, which can kill various unmodified buffers at once. You just need to place your current buffer to the buffer that has the name that you want to kill, and apply that command.


(require 'cl)

(defun batch-kill-buffer-equal-named ()
  (interactive)
  (let ((cbn (file-name-nondirectory
              (buffer-file-name
               (current-buffer)))))
    (let ((eqn-nm (lambda (x)
                    (and (not (eq (buffer-file-name x) nil))
                         (and (string= (file-name-nondirectory
                                        (buffer-file-name x))
                                       cbn)
                              (not (buffer-modified-p x))))))
          (tmp-buffer-list (copy-tree (buffer-list))))
      (dolist (cb (remove-if-not eqn-nm tmp-buffer-list))
        (kill-buffer cb)))))

The command above uses the standard Emacs Lisp functions, also some Emacs CL (Common Lisp) functions to make some tasks easier. So, you need to use the cl package. The other command, applies the same logic, but instead of asking for buffer modifications, it saves the visited buffer and then kills it.


(defun batch-kill-buffer-save-equal-named ()
  (interactive)
  (let ((cbn (file-name-nondirectory
              (buffer-file-name
               (current-buffer)))))
    (let ((eqn-nm (lambda (x)
                    (and (not (eq (buffer-file-name x) nil))
                         (and (string= (file-name-nondirectory
                                        (buffer-file-name x))
                                       cbn)))))
          (tmp-buffer-list (copy-tree (buffer-list))))
      (dolist (cb (remove-if-not eqn-nm tmp-buffer-list))
        (progn (save-buffer cb)
               (kill-buffer cb))))))

This is one the reasons why I am very pleased to use Emacs, you can extend it and customize it without complex tasks, you just need to know how to program using the Emacs Lisp dialect. Then you can place those commands on binding of your taste, I have those commands configured with the following bindings:


(global-set-key "C-cC-q" 'batch-kill-buffer-equal-named)
(global-set-key "C-cC-a" 'batch-kill-buffer-save-equal-named)

I think that you will enjoy your favourite editor… Emacs :)


No coments yet.

post a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>