开发者

Emacs question - hash key

开发者 https://www.devze.com 2023-01-20 19:08 出处:网络
I have a Mac Laptop and I am connecting toserver 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 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!


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 "#")))


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.


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


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 "#"))


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)


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 @?

0

精彩评论

暂无评论...
验证码 换一张
取 消