2014-04-04 Fast buffer switching and friends

It’s been a while since I posted here – not because I have nothing to write about (after all, the world is full of interesting stuff!), but because of lack of time. Today I decided to share a little Elisp hack. I’m tired of pressing C-x C-b RET to switch between two buffers I’m working on (and I don’t want to have two windows open very often on a small netbook). Also, I’m tired of accidentally hitting C-z (OK, that does not happen very often, but still – C-z is basically useless, especially that C-x C-z does the same). Anyway, I decided to do something about it. Here’s what I have now in my init.el:

(define-prefix-command 'ctl-z-map)
(global-set-key (kbd "C-z") 'ctl-z-map)
(global-set-key (kbd "C-z C-c") 'compile)
(global-set-key (kbd "C-z C-b") 'switch-bury-or-kill-buffer)

(defun switch-bury-or-kill-buffer (&optional aggr)
  "With no argument, switch (but unlike C-x b, without the need
to confirm).  With C-u, bury current buffer.  With double C-u,
kill it (unless it's modified)."
  (interactive "P")
   ((eq aggr nil) (switch-to-buffer (other-buffer)))
   ((equal aggr '(4)) (bury-buffer))
   ((equal aggr '(16)) (kill-buffer-if-not-modified (current-buffer)))))

(BTW, this also binds compile to C-z C-c.) Now I can press C-z C-b to switch to the other-buffer (so basically, I can easily cycle between the last two buffers), C-u C-z C-b to bury the current buffer (very useful, especially when clocking in Org-mode, for instance), and C-u C-u C-z C-b to kill the current buffer (but only if it’s not modified).

Note also that I had a problem with naming the argument to switch-bury-or-kill-buffer. Finally, I decided that “aggr” (as in “aggressiveness”) is a good idea: the least aggressive variant (no prefix argument) just punches the current buffer a bit, so that it falls behind the next one; the more aggressive one punches it stronger, so that it falls at the end of the buffer list; and the strongest one just kills it;).

CategoryEnglish, CategoryBlog, CategoryEmacs