When I want to do something in another buffer and then restore the original, I currently do this:
(let ((this-buffer (current-buffer)))
(switch-to-buffer (other-buffer))
(insert "I was here!")
(switch-to-buffer this-buffer))
Is there a less verbose way to this, something like:
(save-buffer-and-switch (other-buffer)
(insert "I was here!"))
switch-to-buffer
is just plain wrong here: it changes which buffer is displayed in the currently selected window, whereas you just want to change the currently selected buffer. IOW instead ofswitch-to-buffer
you would wantset-buffer
. SeeC-h f switch-to-buffer RET
.