I am customizing emacs X11 on a Linux box. By default, C-x C-s is bound to save-buffer, and C-x C-s shows as the keyboard shortcut in the File->Save menu. If I put into my .emacs file the following line:

(global-set-key (kbd "s-s") 'save-buffer)

then both C-x C-s and s-s are bound to save-buffer, but C-x C-s shows in the menu as the shortcut. If I put into .emacs:

(global-unset-key (kbd "C-x C-s"))
(global-set-key (kbd "s-s") 'save-buffer)

then only s-s is bound to save-buffer and it shows in the File->Save as the keyboard shortcut. Now if I put into my .emacs the following lines:

(global-unset-key (kbd "C-x C-s"))
(global-set-key (kbd "s-s") 'save-buffer)
(global-set-key (kbd "C-x C-s") 'save-buffer)

only s-s ends up being bound to save-buffer (and it shows in the File->Save menu as the keyboard shortcut), while C-x C-s ends up being unbound - as if the third line was not there. I was not expecting this behaviour. What am I missing?

  • 1
    Your last paragraph: Are you sure that C-x C-s is not bound to save-buffer? The fact that it does not show in the menu does not mean that it is not bound to the command. – Drew Aug 1 '17 at 23:48

You can ask Emacs to show a particular key binding, among those that are bound to a given command, next to a menu item for that command, by using symbol property :advertised-binding.

This tells Emacs to use s-s as the advertised key binding, so that when save-buffer is used in a menu item the key binding shown next to it is s-s:

(put 'save-buffer ':advertised-binding (kbd "s-s"))

See the Emacs manual, node Menu Bar for more information.

  • Thank you very much. This is in fact exactly what I was trying to do. However, when I put this line into the emacs init file (including adding the missing ")" at the end of the line), after the line binding s-s to save-file, it seems to have absolutely no effect. In particular, the menu still advertises "C-x C-s" as the advertised binding, although describe-key confirms that both C-x C-s and s-s are bound to save-buffer. I have tried emacs 24.5.1/GTK+ version 3.22.11 and emacs 25.1.1/GTK+ version 3.22.12, and the behaviour is the same in both cases. – user52932 Aug 2 '17 at 14:58
  • I think it needs to be :advertised-binding rather than 'advertised-binding, that way it seems to work. Thanks again. – user52932 Aug 2 '17 at 15:04
  • @user52932: Right you are. I corrected it to include the colon prefix. Thx. – Drew Aug 2 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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