I tried my best to find a solution to my problem in internet. I gave up looking. I am trying learning emacs on a mac. There are symbols I normally type by using the alt key. However in emacs I do not have access to them (in my german keyboard layout, @ is accessed by alt-l) How can I access these symbols in emacs?
2 Answers
This is simple: set the Cmd key as Meta
and let Alt
key as Alt in your init.el
file - use these:
(setq ns-alternate-modifier 'alt
ns-command-modifier 'meta
ns-function-modifier 'hyper
ns-right-alternate-modifier nil) ;; Act as AltGR, for "|"
Take care of right Alt, it may be modified by lsp-mode
if you use it.
These are for German keyboard, not German Standard.
-
Great! It worked perfectly! Many thanks. I am now learning customizing init.el files.– PhaernosCommented Nov 2, 2021 at 19:04
-
I tried this in an American Keyboard using Brazilian Portuguese Input... But it did not workout: emacs.stackexchange.com/questions/72953/… Commented Aug 23, 2022 at 19:52
The suggestion by @Ian did not help on my system, a MacBook with Ubuntu installed. Here is my solution.
German keyboards of Macs lack frequently used keys like backslash, the pipe symbol, or curly and square brackets. Under MacOs, these are mapped to Alt-5
to Alt-9
, where Alt
is the left Alt-key. Ubuntu, however, maps them by default to the right Alt-key, which is not only confusing when using both MacOs and Linux, but even difficult to enter (the backslash is Alt-Shift-7
). I have thus mapped the modifier key to the left Alt-key for compatibility with MacOs with the following entries in .Xmodmap
:
clear mod1
clear mod5
keycode 108 = Alt_L
keycode 64 = ISO_Level3_Shift
add mod5 = ISO_Level3_Shift Meta_L
add mod1 = Alt_L Mode_switch
This works for every application except Emacs, which apparently maps Alt-combinations to something different (which seems to be just nothing, but this prevents entering special characters). To make these characters work, I had to explicitly bind them in .emacs
with
(global-set-key (kbd "M-7") (lambda () (interactive) (insert "|")))
(global-set-key (kbd "M-/") (lambda () (interactive) (insert "\\")))
(global-set-key (kbd "M-5") (lambda () (interactive) (insert "[")))
(global-set-key (kbd "M-6") (lambda () (interactive) (insert "]")))
(global-set-key (kbd "M-8") (lambda () (interactive) (insert "{")))
(global-set-key (kbd "M-9") (lambda () (interactive) (insert "}")))
(global-set-key (kbd "M-l") (lambda () (interactive) (insert "@")))
Which key code actually is required by the kbd
function can be figured out within Emacs with Ctrl-h k
and then entering the key combination.