I am not sure that this possible but I have I two splitted panes while using tmux inside iTerm2 and I am using emacs daemon. I am using iTerm's keybinding to switch between tabs.

One one of them emacs is open and on the another one shell is open.

|file  |$     |
|      |      |
|      |      |

I usually make a change on the file and switch to other tab (that is on shell) to run the test.

  • During this whenever I switch to other tab is it possible for emacs to save the file automatically triggered by the iTerm's keybinding to switch between tabs without using C-x C-s?

  • If not from the shell can I force emacs save all its open files right before running its tests?

3 Answers 3


The short answer is that Emacs running in a terminal cannot observe GUI events such as switching to another tab. If you want Emacs to observe GUI events, run a GUI Emacs.

(This may be possible using a companion program, but even so this would depend to some extent on the goodwill of iTerm2. If it's at all possible, it's not easy and may have side effects in conditions that you may or may not care about such as attaching Emacs windows to terminals provided by different terminal emulators.)

On the other hand, it's pretty easy to remotely ask Emacs to save. This assumes that you're running the Emacs server: either start Emacs with emacs --daemon or run (server-start) from your init file. To remotely ask Emacs to save all buffers, run the shell command

emacsclient -e '(save-some-buffers t)'
  • Can we run emacsclient -c '(save-some-buffers t)' without opening the emacs's buffer window and just do the saving?
    – alper
    Jul 28, 2021 at 23:05
  • @alper That doesn't open a window. Except it's -e, not -c, sorry. Jul 29, 2021 at 8:19
  • emacsclient -e '(save-some-buffers t)' does not do anything and freezes on my end
    – alper
    Jul 29, 2021 at 9:16
  • @alper Are you running the server? Does Emacs display any messages (check the *Messages* buffer)? What version of Emacs are you running? Jul 29, 2021 at 9:21
  • Yes sir I am running emacs daemon on the background as emacsclient -qut -e filename.txt ; I believe this automatically starts running emacs daemon on the backgorund. I cannot check *Messages* section. Running emacsclient -e '(save-some-buffers t)' just freezes in the terminal and does not do anything
    – alper
    Jul 31, 2021 at 8:48

Take a look at after-focus-change-function. You can advise this function with an :before function to save all buffers. That's easily accomplished by using an anonymous function like:

(lambda () (save-some-buffers t))

like @alper suggested. You can use the advice-add function or the define-advice macro like so

(advice-add 'after-focus-change-function :before #'(lambda () (save-some-buffers t)))


(define-advice after-focus-change-function (:before ())
  (save-some-buffers t))

super-save-mode to saves the buffer automatically when focus moves away from it.

I do not use emacs in terminals, so I do not know if this works out of the box there, but I am pretty sure it can be configured to do what you want.

  • It didn't work for the tab toggle in between the iTerm tabs
    – alper
    Oct 30, 2020 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.