Remap “cmd”+“W” to kill-buffer in Aquamacs?

1

In Aquamacs 3.2, I want to remap cmd+W so it will kill the current buffer instead of closing the frame. I use Aquamacs in a classic Emacs style with one window, so I close buffers far more often than I close windows.

I tried this in my .emacs to no effect.

(global-unset-key [?\s-w])
(define-key global-map [?\s-w] 'kill-buffer)

I also tried a variation on this answer but it still kills the window.

(defun kill-current-buffer ()
  (interactive)
  (kill-buffer (current-buffer)))
(global-unset-key (kbd "s-w"))
(global-set-key (kbd "s-w") 'kill-current-buffer)

I also tried putting that code into ~/Library/Preferences/Aquamacs Emacs/Preferences.el as suggested in this answer.

I've verified with describe-function that it is loaded.

kill-current-buffer is an interactive Lisp function in `.emacs'.

It is bound to s-W.

(kill-current-buffer)

Not documented.

Schwern

Posted 2014-03-31T00:10:37.827

Reputation: 1 978

Have you been able to establish if any preference settings that you might add are being read in? – JonathanS – 2015-03-31T23:23:22.803

@JonathanS Yes, .emacs and ~/Library/Preferences/Aquamacs Emacs/Preferences.el are definitely being read in as I have other preferences in them. Even if I use eval-region on the code it does not work. I verified with describe-function that it is loaded. – Schwern – 2015-04-01T20:49:46.363

Answers

1

There are two things going on. First, Aquamacs has remapped the Mac OS X command key to alt. So it should be (global-set-key '[(alt w)] 'kill-current-buffer).

But that gives a warning because of the second thing, osx-key-mode has defined its own osx-key-mode-map. Emacs suggests to redefine that key in that keymap with define-key.

(defun kill-current-buffer ()
  (interactive)
  (kill-buffer (current-buffer)))
(when (boundp 'osx-key-mode-map)
    (define-key osx-key-mode-map '[(alt w)] 'kill-current-buffer))

Schwern

Posted 2014-03-31T00:10:37.827

Reputation: 1 978

Great. How did you determine that OS X command key had been remapped to alt? – JonathanS – 2015-04-01T22:01:51.270

@JonathanS I realized the function was being loaded, but the problem might be that my key binding was wrong. In searching for information about key bindings someone mentioned remapping the Command key to Alt. I tried it and it worked. – Schwern – 2015-04-02T05:04:11.747