1

I use both GNU Emacs for OSX and Aquamacs. I like to define key bindings that utilize the Mac command key .

Unfortunately, the syntax for specifying the key is different for each.

In Aquamacs, it looks like this:

(define-key osx-key-mode-map (kbd "A-h") 'replace-string)

In GNU Emacs for OSX, it looks like this:

(define-key key-minor-mode-map (kbd "s-h") 'replace-string)

Is there a way to specify these key bindings in a way that both GNU Emacs and Aquamacs will understand, so that I don't have to maintain and update two separate .emacs files every time I add a new key binding?

incandescentman
  • 6,168
  • 3
  • 46
  • 86
  • I don't use Aquamacs, but I believe the Mac command key *is* the meta key (skim [this tutorial](http://ergoemacs.org/emacs/keyboard_shortcuts.html)). It seems like you should just be able to `(global-set-key (kbd "M-h") 'replace-string)`. – Dan Sep 02 '14 at 19:06
  • No, they are two separate keys. You're thinking of the option key. – incandescentman Sep 03 '14 at 07:22

2 Answers2

2

This is something I borrowed from here: http://www.hulubei.net/tudor/configuration/download/.emacs

I have not tested the code, but it looks like it should work. With this type of setup, the original poster can maintain just one .emacs file and/or load the preferences file.

(defvar gnuemacs-flag (string-match "GNU" (emacs-version)))

(defvar aquamacs-flag (string-match "Aquamacs" (emacs-version)))

(cond
  (aquamacs-flag
    (define-key osx-key-mode-map (kbd "A-h") 'replace-string))
  (gnuemacs-flag
    (define-key key-minor-mode-map (kbd "s-h") 'replace-string)))
lawlist
  • 13,099
  • 3
  • 49
  • 158
2

Based on lawlist suggestion, you could even define a function to do it:

(defvar gnuemacs-flag (string-match "GNU" (emacs-version)))
(defvar aquamacs-flag (string-match "Aquamacs" (emacs-version)))

(defun define-hyper-key (key fun)
  (cond
   (aquamacs-flag
    (define-key osx-key-mode-map (kbd (concat "A-" key)) fun))
   (gnuemacs-flag
    (define-key key-minor-mode-map (kbd (concat "s-" key)) fun))))

;; The aquamacs/gnuemacs keybindings:
(define-hyper-key "h" 'replace-string)
duthen
  • 848
  • 6
  • 14