Switch to Previous Buffer

Jumping between the current buffer and the one you were in before is a pretty common task. It’s also one you can solve in a multitude of way - the most popular being simply the use of switch-to-buffer and ido-switch-to-buffer. Both commands (which are generally bound to C-x b) would suggest to select the previous buffer when you invoke them. While this was good enough for me for many years, recently Luke Randall suggested me a neat alternative:

(defun er-switch-to-previous-buffer ()
  "Switch to previously open buffer.
Repeated invocations toggle between the two most recently open buffers."
  (interactive)
  (switch-to-buffer (other-buffer (current-buffer) 1)))

When you pair this command with a handy key chord it becomes a great performance booster:

(key-chord-define-global "JJ" #'er-switch-to-previous-buffer)

If you’re not fond of key chords I’d suggest a good old keybinding instead:

(global-set-key (kbd "C-c b") #'er-switch-to-previous-buffer)

The command and its key chord are part of Prelude.

Comments