.emacs.d / files / ibuffer.el

(require 'ibuffer)
;; filter groups
(setq ibuffer-saved-filter-groups
      (quote (("default"
               ("Org" ;; all org-related buffers
                (mode . org-mode))
                (or (predicate
                     (let ((bfn (buffer-file-name (current-buffer))))
                       (when bfn
                         (and (string-match-p "\\.emacs\\.d" bfn)
                              (eq major-mode 'emacs-lisp-mode)))))))
                (or (mode . emacs-lisp-mode)
                    (mode . lisp-interaction-mode)
                    (mode . inferior-emacs-lisp-mode)))
                (or (mode . tex-mode)
                    (mode . plain-tex-mode)
                    (mode . latex-mode)))
               ("Markdown" (or (mode . markdown-mode)
                               (mode . gfm-mode)))
                (or (mode . html-mode)
                    (mode . css-mode)
                    (mode . php-mode)
                    (mode . js-mode)))
                (mode . dired-mode))
                (or (mode . image-dired-display-image-mode)
                    (mode . image-dired-thumbnail-mode)
                    (mode . image-mode)))
                (or (name . "tramp")))
               ("Programming" ;; prog stuff not already in MyProjectX
                 (mode . c-mode)
                 (mode . perl-mode)
                 (mode . python-mode)
                 (mode . cc-mode)
                 ;; etc

;; (define-ibuffer-filter in-directory
;;   "Toggle current view to buffers whose default-directory is in QUALIFIER."
;;   (:description "in-directory"
;;    :reader (read-directory-name "Directory: "))
;;   (with-current-buffer buf (file-in-directory-p default-directory qualifier)))

(define-ibuffer-column size-h
  (:name "Size"
   :inline t
   (lambda (column-strings)
     (let ((total 0))
       (dolist (string column-strings)
         (setq total
               ;; like, ewww ...
                (let ((number (float (string-to-number string))))
                   ((string-match-p "K" string)
                    (* number 1000))
                   ((string-match-p "M" string)
                    (* number 1000000))
                   (t number)))
       (file-size-human-readable total 'si))))
  (file-size-human-readable (buffer-size) 'si))

;; Modify the default ibuffer-formats
(setq ibuffer-formats
  '((mark modified read-only
          " " (name 25 25 :left :elide)
          " " (size-h 9 -1 :right)
          " " (mode 16 16 :left :elide)
          " " filename-and-process)
    (mark " " (name 16 -1)
          " " filename)))

;; startup function
(defun customize-ibuffer-mode ()
  "Startup function."
  (ibuffer-switch-to-saved-filter-groups "default")
  (add-to-list 'ibuffer-hidden-filter-groups "Tramp")
  (visual-line-mode -1)
  (toggle-truncate-lines 1))
(add-hook 'ibuffer-mode-hook 'customize-ibuffer-mode)

;; Switching to ibuffer puts the cursor on the most recent buffer
(defadvice ibuffer (around ibuffer-point-to-most-recent activate) ()
  "Open ibuffer with cursor pointed to most recent buffer name"
  (let ((recent-buffer-name (buffer-name)))
    (ibuffer-jump-to-buffer recent-buffer-name)))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.