Unable to type braces and square braces in emacs

26,935

Solution 1

(setq mac-option-modifier nil
      mac-command-modifier 'meta
      x-select-enable-clipboard t)

This is what I use for my swedish keyboard. It even works with svorak A5, if you use it :)

Solution 2

You could also try:

(setq mac-option-key-is-meta t)
(setq mac-right-option-modifier nil)

Solution 3

I'm assuming that you're using a graphical emacs, and not just using the OS X bundled version from within Terminal.

To ensure that Emacs responds to keystrokes in the same way as other OS X apps, try the following:

(setq default-input-method "MacOSX")

And in particular, if you want to use the Option key to enter extended characters not on your keyboard (e.g. "Option-c c" => "ç"), use these settings:

(setq mac-command-modifier 'meta)
(setq mac-option-modifier 'none)

(Put these commands in your ~/.emacs or ~/.emacs.d/init.el emacs startup file, and restart Emacs, or just "M-x eval-buffer" while editing the file.)

Solution 4

(setq default-input-method "MacOSX")

(setq mac-command-modifier 'meta
      mac-option-modifier nil
      mac-allow-anti-aliasing t
      mac-command-key-is-meta t)

Try this. You will be able to use the Alt key as a AltGR and for all the old M-x functions you will have to use your command key.

Solution 5

Bind the relevant keyboard shortcuts to anonymous functions that insert those characters, for example add these lines to ~/.emacs for European Portuguese:

(global-set-key "\M-(" (lambda () (interactive) (insert "{")))
(global-set-key "\M-)" (lambda () (interactive) (insert "}")))

(global-set-key "\M-8" (lambda () (interactive) (insert "[")))
(global-set-key "\M-9" (lambda () (interactive) (insert "]")))

Then save ~/.emacs with C-x C-s and reload it with M-x load-file and type ~/.emacs.

One downside is that this does not work in the mini-buffer, and typing "Alt-9" will insert text in the buffer and not the mini-buffer.

Comparison with other solutions: This solution maintains compatibility with other shortcuts using M-. The solutions by @monotux, @sanityinc, and Abdul Bijur V A do work, but they do not maintain that compatibility, e.g. Cmd-Q no longer quits the program and M-x no longer calls the mini-buffer to execute commands.

The solution by @patrikha doesn't suit touch-typing, which requires the same modifier commands on the right and the left side of the keyboard (Command, Alt/Option, Shift, and Control). For example, with this solution doing M-x requires the left thumb on the left Alt key and the left index finger on the S key, instead of the right thumb on the right Alt key. You could (setq mac-left-option-modifier nil), but that might require a change in habits for letters on the right side of the keyboard.

Notes: If you use AquaMacs, the wiki has a work-around in the section "Inputting {}[] etc. on non-English keyboards, or other keys with the Option modifier".

I also add this line to the end of ./emacs to show the matching of brackets and braces: (show-paren-mode).

Share:
26,935

Related videos on Youtube

Niclas Åstrand
Author by

Niclas Åstrand

Updated on August 13, 2020

Comments

  • Niclas Åstrand
    Niclas Åstrand almost 4 years

    I'm running Mac OS X and GNU Emacs 22.3.1. I use a swedish keyboard. I am unable to type braces { }, [ ] in emacs. When trying to type braces I get parenthesis. Since I'm quite new to Mac and emacs I need a little help on configuring emacs to get this right.

    • Jonathan Allen
      Jonathan Allen almost 14 years
      What you call "square braces" are usually refered to as "brackets". Not that it matters, but its a bit shorter to type.
    • user276641
      user276641 almost 14 years
      Watch out: "brackets" is a regional one. In the US it means square brackets, in the UK, it means round brackets / parenthesis.
    • eriq
      eriq almost 10 years
      Unfortunately not one of the answers solved a similar issue on my german keyboard. I don't get any brackets or curly braces at all.
    • nha
      nha almost 9 years
      @sanityinc solved it for me on my French keyboard... however I am not satisfied at all, since to make a "[" I have to press three keys (alt shift 5) !
  • muxare
    muxare over 12 years
    I tryed putting this into bouth my ~/.emacs file and my ~/.emacs.d/init.el file but i can't get it to work.
  • monotux
    monotux over 12 years
    Are you sure you're on a mac? ;)
  • monotux
    monotux over 12 years
    srsly tho, try emacs -Q and then paste the above into *scratch*, eval with M-x eval-buffer and see if it works. It could be your other settings that screws something up.
  • muxare
    muxare over 12 years
    I just tryed "emacs -Q and then paste the above into scratch, eval with M-x eval-buffer". But the curly bracets dont work.
  • monotux
    monotux about 10 years
    I'm guessing that the above code isn't evaluated properly. Try adding it to your *scratch*-buffer, evaluate it and see if it works. If it does work, try removing all your emacs settings (moving .emacs.d somewhere) and start again.
  • leontalbot
    leontalbot almost 9 years
    This is fantastic, but I now have an issue after relaoding emacs. Can't type letter "c" anymore. The uppercase "C" works but not lowercase. Anything I can do?
  • leontalbot
    leontalbot almost 9 years
    I just opened a question for my issue : stackoverflow.com/questions/31524961/…
  • monotux
    monotux over 8 years
    <kbd>C-a =</kbd> is not a standard shortcut. I don't know what you've done in your config but it's most likely unrelated to the above.
  • AdrieanKhisbe
    AdrieanKhisbe over 8 years
    why didn't I discovered that earlier? :D
  • stackdaemon
    stackdaemon almost 8 years
    Yes! The setq mac-right-option-modifier nil is what totally saved me! Now I can have my left alt as meta and my right alt as curly brackets source. Thank you!
  • Teemu Leisti
    Teemu Leisti almost 8 years
    Works for me, too. Actually, didn't need the first line.
  • Mankka
    Mankka over 7 years
    I didn't need the first line either.
  • Pelican
    Pelican about 3 years
    @monotux I will just leave it in the scratch as it seems to be working correctly this way, and I never close emacs. Thx
  • monotux
    monotux about 3 years
    @Pelican you can paste them into your ~/.emacs.d/init.el instead, then they are evaluated every time you start Emacs.
  • Pelican
    Pelican about 3 years
    @monotux i never « start » eMacs ... I live in there 😅, all jokes aside yes I will do that, thank you