I have started to use emacs with a macbook and the first difficulty was that the meta keys are used for emacs things, where there would otherwise be used for special characters, such as: [, ], |, {, }, ...

[Not that it changes much, but just to mention it, my macbook has a German keyboard]

To solve this I had defined my own functions for each special character, which lives in my .emacs file. For example, to insert a pipe:

(defun nwm/open-curley-bracket ()
  "A custom function to insert an opening curley bracket as the mac key-bind for it doesn't work within emacs."
  (insert "\{"))
(global-set-key (kbd "M-8") 'nwm/open-curley-bracket)


I would like to use electric-pairs to auto-complete these special characters, meaning when I type { I expect both braces to appear {} with the cursor left between them. The electric pairs mode works nicely for the special characters like normals brackets () that I do not need to remap to my keyboard, however it doesn't work for my new keybound characters like the pipe above.

I have tried setting the required special characters with less convoluted methods, e.g.:

(define-key global-map (kbd "M-8") "{")

... this still doesn't work for me.

I don't know much about Lisp, so if anybody can interpret the colde bhind electric-pairs to find the cause of the problem, please do! http://www.emacswiki.org/emacs/ElectricPair

Has anybody got a work-around or a different solution?

  • 1
    Hasn't it appeared to you that | is not paired character? What do you want electric-pair to do? Insert one more pipe? Sep 17, 2015 at 21:21
  • Ok, that way a bad example with the pipe on my part (I'll edit it). However, it doesn't work for any of the brackets that I needed to remap myself, so not for [, ], {, }. It also doens't work for ', which it should do according to the documentation, I believe.
    – n1k31t4
    Sep 17, 2015 at 21:29

3 Answers 3


Personally, I don't like the default way Emacs on OS X use the modifier keys. I have the following in my init file, it places meta on Cmd and restore Alt to the normal OS X behaviour to access special keys. This way it just work with electric-pair-mode and you can drop all your special functions.

(if (boundp 'ns-command-modifier)
    (setq ns-command-modifier 'meta))

(if (boundp 'ns-option-modifier)
    (setq ns-option-modifier nil))
  • I had seen this workaround of yours elsewhere... it appeals to me a lot more now though, after wasting time with other solutions! I think I was worried about losing other functionality that is in my Mac-Edition of Emacs, which means things like s-c and s-v do copy/paste and s-s saves the file etc. I will try it out and see how I get on for now...
    – n1k31t4
    Sep 17, 2015 at 21:36
  • I like to use the right option/alt for the default OSX stuff like creating Spanish vowels/accents; and I use the left option/alt for Meta. I have disabled the escape key as Meta, and the escape key is a separate prefix/modifier key with its own map.
    – lawlist
    Sep 17, 2015 at 22:46
  • @lawlist - would you mind providing your method or a source on how you separated the left and right option keys? I now have the caps-lock key as an extra control, the fn key as the normal command-key (which makes it super), and it all feels pretty good. I still am not getting autocomplete on {though, which was my original question here. I am wondering whwere is the best place for a hyper key now though, so splitting left-right option keys would be useful!
    – n1k31t4
    Sep 17, 2015 at 22:55
  • I am not familiar with the question in this thread, but here is my setup -- it is mostly stock, with the exception of the right option/alt, which does the default OSX thing for stuff like Spanish characters on an English keyboard: (setq ns-alternate-modifier 'meta) (setq ns-right-alternate-modifier 'none) (setq ns-command-modifier 'super) (setq ns-right-command-modifier 'left) (setq ns-control-modifier 'control) (setq ns-right-control-modifier 'left) (setq ns-function-modifier 'none) I've been using Emacs for 2.5 years -- OSX and Windows -- but have never used hyper and am unsure how.
    – lawlist
    Sep 18, 2015 at 0:18
  • Thanks lawlist - What do the leftflags do exactly? E.g. (setq ns-right-command-modifier 'left). I know that if you do not specify left or right, then the default is left, but it seems you are maybe just assigning the right side buttons to be the same as the left sides...? I have something similar going on now. If you are interested, here is how I add a hyper function key to my fn on my mac keyboard: '(if (boundp 'ns-function-modifier) (setq ns-function-modifier 'hyper))` It provides you with another key like control, meta & super to make custom keybindings with.
    – n1k31t4
    Sep 18, 2015 at 0:48

electric-pair-mode uses post-self-insert-hook to do the pairing. Inserting a string into the buffer with insert is not going to trigger the post-self-insert-hook because you never ran self-insert-command.

Instead of using insert use execute-kbd-macro to treat the insertion as if you had pressed the key for a curly bracket.

(defun my-insert-lcurly ()
  (execute-kbd-macro "{"))

Depending on your major-mode, your syntax table might not recognize certain characters as bracket pairs. The syntax for the characters you want: (, {, [, etc need to be set accordingly. For things like pipe characters, you may want to modify the value of electric-pair-pairs to include them in order the the mode to work.

  • Shouldn't this work the same as binding to "{"?
    – npostavs
    Sep 18, 2015 at 15:36
  • I added you suggestion to my .emacs file, but unfortunately it still doesn't auto-pair the curley braces. I have found an interesting artefact though... I the curley braces are entered within a preexisting string with i.e. within "", then they are auto-paired! Also, they are autocompleted in other mode such as in Python and org-mode (All of this without your workaround, straight from electric-pair). Any ideas why lisp just sees { as another character/variable and not as a character like (, [, or " - which all auto-pair within lisp?
    – n1k31t4
    Sep 18, 2015 at 18:35
  • Because curly braces are just another character in lisp. They have no special meaning. They are no different than other characters you can use for symbol names. For instance try evaluating this: (let (({{{ 3) (} 5)) (+ } {{{)) It is totally valid. There should be no assumption that curly braces are matched in lisp no more than there should be an assumption that every A should have a matching dollar sign. Sep 18, 2015 at 18:49
  • Ok, this now makes ore sense. I had understood that electric-pair would be a global method so simply auto-pair each of the bracket types as well as speech marks, regardless of the mode. I mean, it doesn't matter which language you use, brackets always come in pairs (with only a few exceptions - e.g. regex stuff).
    – n1k31t4
    Sep 18, 2015 at 20:09

Emacs has a whole stack of translation mechanisms going from the keyboard events it receives from the operating systems to the commands that they trigger. If you want a keychord to insert a character, the global keymap is too high level. You want one of the translation keymaps. If you want Meta+8 to always be treated as a { key, then declare the binding in key-translation-map.

(define-key key-translation-map [(meta ?8)] "{")

The fact that you expect M-8 to insert a character may indicate that your modifier setup is wrong, though. Normally, the keychord to insert { would be AltGr+8; if Emacs sees your AltGr key as Meta, you should probably instruct it otherwise.

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.