Shuguang
Shuguang

Reputation: 2071

How to kill all buffers with buffer file in a certain location?

How to kill all buffers with buffer file in a certain location?

For example, I want kill all buffers with buffer file in c:...\dev and all dired buffer subdir to c:...\dev

Or in others words, how to go though the files or dirs of all buffers in emacs?

Thanks @David @phils. I combined the elisp code and function in ibuffer

(mapc (lambda (buffer)
    (let ((file-name
           (or (buffer-file-name buffer)
               (with-current-buffer buffer
                 (and
                  (boundp 'dired-directory)
                  (stringp dired-directory)
                  dired-directory))
               )))
      (when (and file-name
                 (string-match "^c:.*?\\\\dev" file-name))
        (kill-buffer buffer))))
  (buffer-list))

Upvotes: 2

Views: 970

Answers (1)

David
David

Reputation: 46

From elisp use:

(mapc (lambda (buffer)
        (let ((file-name
               (or (buffer-file-name buffer)
                   ;; In dired-mode we need `dired-directory' which
                   ;; might be a list and may not be fully expanded.
                   (with-current-buffer buffer
                     (and (eq major-mode 'dired-mode)
                          (expand-file-name
                           (if (consp dired-directory)
                               (car dired-directory)
                             dired-directory)))))))
          (when (and file-name
                     (string-match "^c:\\\\.*\\\\dev" file-name))
            (kill-buffer buffer))))
      (buffer-list))

Upvotes: 3

Related Questions