How to write a tilde (~) character in Emacs on Mac OS X?
Asked Answered
J

6

7

I'm used to write a ~ character by pressing Alt+N on Mac OS X. This does not work in Emacs. Alt+N key seems to be bind to the command history. So my question is how to write a ~ character in Emacs on Mac OS X?

EDIT: I'm using Aquamacs.

Jacklin answered 20/6, 2012 at 19:58 Comment(3)
Which keyboard and keyboard layout are you using? English ones at least have it above the backtick `.Timeout
I'm using an Apple Keyboard with german layout.Jacklin
Option+n does not insert a tilda (~) on MacOS by default. Instead it is a combining character, creating e.g. ñ (Opt+n n) or a small tilda ˜ (Opt+n <space>).Gemoets
C
2

You could always open the 'character viewer', select 'Punctuation', find '~' (tilde), and then double click it. That will insert it at the Emacs point. (The 'character viewer' is readily accessible after checking 'Show Keyboard & Character Viewers in menu bar' from the Keyboard pane in the System Preferences window.)

You could also define an emacs-lisp function as:

(defun tilde () (interactive) (insert "~"))

and then invoke it with M-x tilde to insert a tilde. Could then assign that function to the key combo of your choice as

(global-set-key "\M-\C-!" 'tilde)   ;; you choose the combo

and add all this to your 'emacs init' file.

Caylacaylor answered 20/6, 2012 at 20:36 Comment(2)
This solution works while in the editor, however it does work in the mini buffer. Any idea how to make it work in it? Can we call such a function from the mini buffer any put the result in the mini buffer (I am quite new to Emacs)?Tristich
IMHO, this is not the answer. The root problem stems from Emacs disabling Alt for entering characters on OS X. See my answer below.Manche
M
15

While this question is pretty old, none of the answers seems satisfying for Emacs for OS X (the most popular choice these days). So, for future readers ...

Plain Emacs for OS X uses both Alt keys as Meta by default. As many characters are typed using Alt on a german Mac keyboard (tilde, brackets, curly braces etc.), I'd recommend setting ns-right-alternate-modifier to nil, which enables typing tilde (Alt-n) and other characters using the right Alt key, while the left one can be used as Meta (like for M-x).

Alternatively Cmd can be customized to be Meta. All options can be interactively customized under M-x customize-group ns.

Manche answered 8/11, 2015 at 21:29 Comment(0)
C
2

You could always open the 'character viewer', select 'Punctuation', find '~' (tilde), and then double click it. That will insert it at the Emacs point. (The 'character viewer' is readily accessible after checking 'Show Keyboard & Character Viewers in menu bar' from the Keyboard pane in the System Preferences window.)

You could also define an emacs-lisp function as:

(defun tilde () (interactive) (insert "~"))

and then invoke it with M-x tilde to insert a tilde. Could then assign that function to the key combo of your choice as

(global-set-key "\M-\C-!" 'tilde)   ;; you choose the combo

and add all this to your 'emacs init' file.

Caylacaylor answered 20/6, 2012 at 20:36 Comment(2)
This solution works while in the editor, however it does work in the mini buffer. Any idea how to make it work in it? Can we call such a function from the mini buffer any put the result in the mini buffer (I am quite new to Emacs)?Tristich
IMHO, this is not the answer. The root problem stems from Emacs disabling Alt for entering characters on OS X. See my answer below.Manche
H
1

Unfortunately the answer by fpbhb does not work when running emacs in a terminal (emacs -nw).

I was able to come up with a solution to this problem that works in both situations (standalone and in-terminal). Also, I have an international keyboard and I was also able to fix the problem of not being able to type special characters when running emacs in the terminal.

This snippet properly binds the left option key to "META" when running emacs as an app (i.e. not in a terminal). It does not bind the right option key, which can be used to type special characters:

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

All of the above has no effect when running emacs in a terminal. To obtain the same key bindings in the Terminal you have to:

Terminal Preferences -> Keyboard -> Use Option as Meta key

Unfortunately, after doing this you will not be able to use the option key to type special characters in international keyboards. In particular I was missing the tilde, the backslash and the @.

I solved this last problem by adding the missing key mapping to my .emacs:

(define-key key-translation-map (kbd "M-ñ") (kbd "~"))
(define-key key-translation-map (kbd "M-º") (kbd "\\"))
(define-key key-translation-map (kbd "M-2") (kbd "@"))

Voilà.

Hypethral answered 26/4, 2017 at 12:12 Comment(0)
G
0

quoted-insert should deal with this.

C-qAlt-N

Glebe answered 20/6, 2012 at 22:39 Comment(1)
Doesn't work for me, I just get "î". My keyboard is belgian-french, but the key combo for the tilde is also Alt-N outside of emacs.Goles
K
0

Aquamacs : Options -> Options Commands Meta Key -> Meta and French ?

Kunz answered 25/2, 2014 at 11:4 Comment(0)
A
0

C-x 8 ' followed by the vowel of your choice does it for me. If you want the tilde to go in the other direction you can replace the ' with a `.

I don't know whether this solution is universal. I'm using Linux.

Amboina answered 6/11, 2023 at 21:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.