Emacs Redux

Return to the Essence of Text Editing

Switch to Previous Buffer

| Comments

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:

1
2
3
4
5
(defun 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:

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

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

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

The command and its key chord are part of Prelude.

Comments