(In case someone wants the sorting, without the column, the built-in s v
will sort by "recency" and should (roughly, see below) do the trick.)
I wanted something like this as well, so I put the above comments to work.
First create the actual column, via define-ibuffer-column
. buffer-display-time
is a local variable, hence with-current-buffer
sets the context. I've formatted it here with my preferred display style (YYYY-MM-DD HH:MM
) but that can obviously be changed; see the the docs on format-time-string
. If you want it relative, you'll have to come up with a more involved, custom function (the definitions of org-evaluate-time-range
and calendar-count-days-region
may provide a jumping off point).
(define-ibuffer-column last-viewed
(:name "Last-viewed" :inline t)
(with-current-buffer buffer
(format-time-string "%Y-%m-%d %R" buffer-display-time)))
Then we need to actually insert the column where you want it in ibuffer-formats
. Here's what I've done, YMMV:
(setq ibuffer-formats
'((mark modified read-only locked " "
(name 18 18 :left :elide)
" "
(size 9 -1 :right)
" "
(mode 16 16 :left :elide)
" "
(last-viewed 18 -1 :left)
" " filename-and-process)
(mark " "
(name 16 -1)
" " filename)))
That's it — the column should appear, and you should be able to sort with s v
!
There's something weird about that "recency" sorting I can't quite figure out — I think it's that it derives the list from buffer-list
, so it interacts poorly with ibuffer-auto-mode
? Regardless, it doesn't seem to reliably sort by buffer-display-time
, so I also created a custom sorter function to override that:
(define-ibuffer-sorter last-viewed
"Sort the buffers by last viewed time."
(:description "last-viewed")
(string-lessp (with-current-buffer (car a)
(format-time-string "%Y-%m-%d %R" buffer-display-time))
(with-current-buffer (car b)
(format-time-string "%Y-%m-%d %R" buffer-display-time))))
(define-key ibuffer-mode-map (kbd "s v") 'ibuffer-do-sort-by-last-edited)