A couple of ideas.
First, if you find yourself invoking a command like save with a sufficiently high frequency, you might consider a shorter key binding for the command. For example, I also found myself having the same "twitch," so now I use f2
instead of C-x C-s
for saving edits.
The function that I bind to f2
saves every unsaved buffer unconditionally. You might find it useful:
(defun force-save-all ()
"Unconditionally saves all unsaved buffers."
(interactive)
(save-some-buffers t))
(global-set-key [f2] 'force-save-all)
Now, on to the main issue. You could try something like this (notice that force-save-all
is called):
(defun my-switch-to-buffer (buffer)
(interactive (list (read-buffer "Switch to buffer: " (cadr buffer-name-history) nil)))
(force-save-all)
(switch-to-buffer buffer))
(global-set-key "\C-xb" 'my-switch-to-buffer)
Of course, you could also bind the switch buffer functionality to another key, like a function key, so that it's a one press operation.
I thought that @seth had a great idea about using advice, but I noticed that the ELisp manual suggests that advice not be used for key bindings. I'm not quite sure why this is the case, but that's what the manual suggests FYI.