33

I have a Mac Laptop and I am connecting to server running Linux. As Alt+3 is already bound in EMACS to a command, so I cannot insert the hash symbol in a file. I have tried the following solution I found online:

(global-unset-key (kbd "C-3"))
(global-set-key (kbd "C-3") '(lambda() (interactive) (insert-string
"#")))      //I know that C is for CTRL not Alt - I have tried with
M-3 instead as well

and some others as well, but none seem to work. Can you tell me any other way in which I might be able to enter the hash sign (#) in a file.

Aso tried (did not work):

(fset 'insertPound "#")
(global-set-key (kbd "M-3") 'insertPound)

Thank you!

Luke Girvin
  • 12,672
  • 8
  • 57
  • 79
Andrei
  • 1,642
  • 3
  • 13
  • 15
  • 3
    Why are you using "Alt+3"? Isn't that the "shift-3" character? – S.Lott Oct 20 '10 at 11:06
  • 2
    Your second solution (from http://stackoverflow.com/questions/1704119/) worked fine for me. Preferable to pretending to be Australian :) – Scott Griffiths Dec 19 '10 at 16:34
  • As other replies have surmised, Andrei has a UK keyboard on his Mac. It's one of the oddities of that layout that you get a '#' (Octothorp/Hash) character with Option+3 (*not* marked on the keyboard) - Shift+3 gives you '£' (GB Pound); and the '€' (Euro) character with Option+2 (marked on the keyboard!) - Shift+2 gives you '@' (AT) – jrg Apr 29 '13 at 10:07

6 Answers6

38

From http://jimbarritt.com/non-random/2010/11/07/typing-the-pound-or-hash-key-in-emacs-on-uk-macbook

Typing the pound, or hash (#) key in emacs on UK Macbook:

The problem with OS X and the UK keyboard is that the pound key actually has a £ on it. To get “#” you have to press Alt+3

Of course, in emacs, the alt key is the meta key which is trapped by emacs. The simple function below inserted into your .emacs file should map the keys correctly.

;; Allow hash to be entered  
(global-set-key (kbd "M-3") '(lambda () (interactive) (insert "#")))
otfrom
  • 474
  • 5
  • 10
Nick Crabtree
  • 381
  • 3
  • 2
  • doesn't work for isearch, though! Anyone ever found a solution for that? – jrg May 16 '15 at 13:06
  • 1
    to follow up, this does the job for isearch: (define-key isearch-mode-map (kbd "M-3") '(lambda () (interactive) (isearch-process-search-char ?\#))) – jrg May 16 '15 at 13:19
34

I assume that you have a Mac UK keyboard so Shift-3 is £. On most other keyboards Shift-3 is # as others have said.

The way I get round it is to change the input source to Australian the only difference is that Shift-3 is now # and Alt-3 is £ (or leave as the emacs binding)

Input Source setting was System Preferences->Language&text->Input Source
On later OSX versions (OSX 10.11 definitely but would have been earlier) Input Source setting is System Preferences->Keyboard->Input Source By default this will just show the UK keyboard to see more hit the + at the bottom of the list and add Australian

The reason I prefer this rather than adding code in emacs is that Shift-3 is # for all apps e.g. including Xcode/Eclipse so I don't have to switch the key according to the app or according to wether I am on a US keyboard or on Windows/Linux etc.

mmmmmm
  • 30,723
  • 26
  • 85
  • 109
  • 1
    +1. I need to become more culturally sensitive and realise that there are keyboards different from my own. :) – Noufal Ibrahim Oct 20 '10 at 11:56
  • 2
    I think the global-set-key method below is better. – otfrom Nov 26 '11 at 16:45
  • @otfrom - for emacs maybe but my suggestion works for other apps e.g. Xcode/Eclipse/vi as well – mmmmmm Nov 26 '11 at 21:03
  • @Mark - Alt+3 already works for adding # in xcode/eclipse/vi/chrome/firefox/etc. The only problem is in emacs where the Alt (Meta) is gobbled up by emacs and it skips self-insert. – otfrom Nov 27 '11 at 16:33
  • 1
    I previously set my Mac to Irish keyboard to get the sensible # character, but somehow that stopped working and started giving £ on Shift-3 instead. But Australian still does the trick. – Ed Avis Jan 17 '15 at 11:17
17

I know this is a bit late and the answer has been accepted. However, I have just moved from Linux to MacOS with a UK keyboard and had the same problem.

Note: I am using the emacs from here: http://emacsformacosx.com/. The below may be different for Carbon Emacs/Aquamacs etc.

The global-set-key method above is fine if you just need the # sign, but what if you also need to access the character? (Which is Alt-3 on a UK keyboard)

The solution for me was to add this to my init file:

(setq ns-right-alternate-modifier (quote none))

This removes the emacs bindings for the right alt/option key.

You can see all the available options with

M-x customize-group RET ns RET

Credit goes to http://emacsformacosx.com/tips

robsearles
  • 366
  • 2
  • 3
4

A lot of the solutions given here and elsewhere work for typing # in a normal buffer, but they don't make it work like a normal keypress; in particular, it will abort an incremental search, which makes it hard to write macros that deal with Python comments, or C #includes, for example. So, it's best to transform the key much earlier, so it just acts like another typing keystroke.

I've found that adding this command to your Emacs configuration works very well:

(define-key key-translation-map (kbd "M-3") (kbd "#"))

...and remove all the (global-set-key...) attempts.

If -- like me -- you switch your modifier keys around, Opt ⌥ is mapped to Hyper, so I just go belt-and-braces with:

(define-key key-translation-map (kbd "M-3") (kbd "#"))
(define-key key-translation-map (kbd "M-£") (kbd "#"))
(define-key key-translation-map (kbd "H-3") (kbd "#"))
(define-key key-translation-map (kbd "H-£") (kbd "#"))
(define-key key-translation-map (kbd "S-3") (kbd "#"))
(define-key key-translation-map (kbd "S-£") (kbd "#"))
gid
  • 183
  • 1
  • 3
  • 9
  • 1
    This is a great answer! Does exactly what I need without flying the Australian flag over my whole mac:-) – pete23 Oct 02 '18 at 07:55
1

My solution (note escape sequence):

;; Even though we may have set the Mac OS X Terminal's Alt key as the emacs Meta key ...                                                                                                                                                                                      
;; ... we want to be able to insert a '#' using Alt-3 in emacs as we would in other programs                                                                                                                                                                                  
(fset 'insertPound "#")
(define-key global-map "\M-3" 'insertPound)
iainH
  • 994
  • 8
  • 17
-1

As S.Lott said, it's S-3 to insert a number sign (or hash, pound, octothrope).

Why do you want to use the meta modifier to insert it? Also, what is M-3 bound to on your setup? You can get it by doing an C-h-k and then hitting the key combination.


Assuming you are referring to Alt properly and that it's setting the Meta modification bit you can shove

(global-unset-key (kbd "M-3"))

into your .emacs and eval it to disable this from happening.

All "normal" keys are bound to self-insert-command. The shift modifier simply upcases the 'key' which is used to call this function so you get a # instead of 3 when you do a S-3.

Also, I still don't understand why you're using Alt rather than shift to display the # symbol. What do you do when you want to type a @?

Noufal Ibrahim
  • 66,768
  • 11
  • 123
  • 160