Emacs question - hash key - macos

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

Related

What is proper way to define define-key in emacs

I am trying to make toggle insert/normal mode in evil in spacemacs.
I have successfully accomplished that with code down there. But LED light next to Caps-lock on my macbook pro 2018 13' stoped working. So i am trying to switch led on my caps-lock with script.
I am using karabiner-elements to remap caps-lock to f13. When emacs records f13 it changes state to evil-normal-state or evil-insert-state.
The problem starts, when i want to add another command to run when f13 aka caps-lock ist pressed. Its (shell-command "/Users/atrumoram/setleds +caps"). Which turns the light on caps-lock or turns it off. I was trying to make my own function defun. But i really cannot make it work together. In the end i would like to have something like this.
This is code that toggles insert/normal mode in evil using capslock
(define-key evil-insert-state-map (kbd "<f13>") 'evil-normal-state)
(define-key evil-normal-state-map (kbd "<f13>") 'evil-insert-state)
In the end i would like to have something like this.
(define-key evil-insert-state-map (kbd "<f13>") 'evil-normal-state (shell-command "/Users/atrumoram/setleds +caps"))
(define-key evil-normal-state-map (kbd "<f13>") 'evil-insert-state (shell-command "/Users/atrumoram/setleds -caps"))
Is there some way somebody can help me out? Looking forward for your ideas.
A key can only be bound to one function at a time, so in order for two things to happen when you press the key, you need to create a function, make it "interactive", and bind that to the key you want.
This function performs the two actions, one after the other:
(defun my-evil-normal-state-and-set-caps-led ()
(interactive)
(evil-normal-state)
(shell-command "/Users/atrumoram/setleds +caps"))
Since it's declared as an interactive function, you can test it with M-x my-evil-normal-state-and-set-caps-led.
Then it's just a matter of:
(define-key evil-insert-state-map (kbd "<f13>") 'my-evil-normal-state-and-set-caps-led)
And vice versa for switching to insert state.

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

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.
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.
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.
quoted-insert should deal with this.
C-qAlt-N
Aquamacs : Options -> Options Commands Meta Key -> Meta and French ?
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à.

Making Aquamacs scrolling more like Emacs

When I used emacs, I used to be able to set the mark and highlight full pages for yanking using C-v, or scroll-up. However, in Aquamacs if I set the mark then hit C-v it loses the mark and stops highlighting. I noticed that in Aquamacs C-v is instead mapped to aquamacs-page-down, so I tried adding the following command to my site file:
(define-key osx-key-mode-map "C-v" 'scroll-up)
and this didn't successfully remap the key. I then tried something similar:
(define-key global-map "\C-v" 'scroll-up)
and still nothing. Aquamacs very stubbornly hangs onto the mapping to aquamacs-page-down. I noticed, however, that there's an additional function, aquamacs-page-down-extend-region, which does exactly what I'm talking about. Its key sequence, however, is , and I have no idea how to input that. I tried "shift-control-v" to no avail.
Has anyone been able to get Aquamacs to scroll pages while maintaining the mark?
I've found a way to get this to work, for posterity's sake.
Paste this into the .emacs file:
;; Enable scrolling to maintain mark if set
(defun scroll-down-maintain-mark ()
(interactive)
(if mark-active
(aquamacs-page-down-extend-region)
(aquamacs-page-down)))
(defun scroll-up-maintain-mark ()
(interactive)
(if mark-active
(aquamacs-page-up-extend-region)
(aquamacs-page-up)))
(define-key global-map "\C-v" #'scroll-down-maintain-mark)
(define-key global-map "\M-v" #'scroll-up-maintain-mark)
hitting C-SPC C-v C-l, the last to recenter screen, seems to show that it does indeed preserve the mark.
and subsequent copy and yank works fine
perhaps this behavior added on newer Aquamacs.

Unable to type braces and square braces in emacs

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.
(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 :)
You could also try:
(setq mac-option-key-is-meta t)
(setq mac-right-option-modifier nil)
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.)
(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.
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).
I would try a Cocoa based emacs ie version 23. For a mac integrated emacs I would try Aquamacs
I had the same issue with a french keyboard. It looks like an Aquamacs issue (Carbon Emacs does not replace { with ()).
The change in emacs above work fine and I could type brackets but I could not use standard shortcuts anymore (Ctrl+C/Ctrl+V for instance).
Aquamacs provides a workaround.
Menu Bar > Options > Option, Command, Meta keys > select ...Meta & French
It worked fine for me. However it may not work for swedish, no swedish keyboard option.
Using Aquamacs:
From the main menu, go to Options - Option, command, meta keys and select "option for composed characters".
The braces and the brackets work as with the standard Mac keyboard.
You don't need to remember those programming like things: Here is the answer. Go to Keyboard preferences, and check the "Show keyboard and character viewer in menu bar". After that, check on the menu bar near the battery meter for the icon and start double clicking any character you want.

Emacs on Mac OS X Leopard key bindings

I'm a Mac user and I've decided to learn Emacs. I've read that to reduce hand strain and improve accuracy the CTRL and CAPS LOCK keys should be swapped. How do I do this in Leopard?
Also, in Terminal I have to use the ESC key to invoke meta. Is there any way to get the alt/option key to invoke meta instead?
update: While the control key is much easier to hit now, the meta key is also used often enough that its position on my MacBook and Apple Keyboard also deserves attention. In fact, I find that the control key is actually easier to hit, so I've remapped my control key to act as a meta key. Does anyone have a better/more standard solution?
Swapping CTRL and CAPS LOCK
Go into System Preferences
Enter the Keyboard & Mouse preference pane
In the Keyboard tab, click Modifier Keys...
Swap the actions for Caps Lock and Control.
Using ALT/OPTION as META
In the menu bar, click Terminal
Click Preferences...
Under the Settings tab, go to the Keyboard tab
Check the box labeled Use option as meta key
That's it! You should be well on your way to becoming an Emacs master!
For reference, here are the key bindings, for moving around text:
⌥ + ← - move left one word
⌥ + → - move right one word
⌥ + delete - back delete one word
Shift + ⌥ + delete - foward delete one word
⌥ + ↑ - move up one paragraph
⌥ + ↓ - move down one paragraph
⌘ + ← - move to start of current line
⌘ + → - move to end of current line
Shift + any of the above extend selection by appropriate amount
Click then drag - select text
Double-click then drag - select text, wrapping to word ends
Triple-click then drag - select text, wrapping to paragraph ends
Shift + Select text with mouse - add to selection (contiguous)
⌘ + Select text with mouse - add to selection (non-contiguous)
⌥ + Drag - select rectangular area (non-contiguous)
⌘ + ⌥ + drag - add rectangular area to selection
Drag selection - move text
⌥ + drag selection - copy text
Ctrl + A - move to start of current paragraph
Ctrl + B - move left one character
Ctrl + D - forwards delete
Ctrl + E - move to end of current paragraph
Ctrl + F - move right one character
Ctrl + H - delete
Ctrl + K - delete remainder of current paragraph
Ctrl + L - center the window on the current line
Ctrl + N - move down one line
Ctrl + O - insert new line after cursor
Ctrl + P - move up one line
Ctrl + T - transpose (swap) two surrounding character
Ctrl + V - move to end, then left one character
Ctrl + Y - paste text previously deleted with Ctrl - K
Add Option to Ctrl + F or Ctrl + B to move a word instead of a character at a time.
The other answer was very complete, but additionally I'd mention I just set the caps lock
key to a second control key instead of swapping them.
Also, you'll notice that the large majority of the text entry fields in Mac OS X
already accept emacs keystrokes (^A beginning of line, ^E end of line, ^P, ^N, ^K, ^Y, etc)
good luck
I really like the answer provided by Kyle Cronin, but I want to add one thing - make sure you select the appropriate keyboard for this to work. If you have an external keyboard plugged into your laptop, then there are is an additional drop down box and you will need to do this for both keyboards (or at least for your external keyboard). The screen shot below shows the "Select Keyboard" dialog box - I have selected "Joint Mac Keyboard", which is MacBook's way of saying GoldTouch external keyboard - the default is the built-in keyboard.
Once I figured that out - this works great for me!
If you use emacs over an ssh connection, or through a machine not on your local computer, the page up/page down buttons scroll through the terminal buffer - in my experience, not too helpful.
You can set your page down and page up buttons to send the appropriate commands to emacs. In emacs, you can scroll through the emacs buffer like so:
Page Up: Ctl-v
Page Down: Esc-v
So, to have the terminal send these commands to emacs, follow the instructions above to alter the Alt keys for Meta. However, instead of setting the "use option as meta" option, find the "page down" and "page up" options.
Page Down
Double click the "page down" option to edit it. Change Action to "send string to shell" and enter \026 as the string. Save it.
Page Up
Double click the "page up" button to edit it. Change Action to "send string to shell" and enter \033v as the string. Save it.
Not sure if you're totally married to using Emacs from the terminal, but another option is to use Carbon Emacs (my favorite) and Aquamacs (very Mac-like). Carbon Emacs uses the command key for meta, this is nice because you can do Control-Meta commands just by holding Control and Command down instead of first hitting escape then the control key sequence.
Also, if you're a serious Emacs user I thoroughly recommend that you get a keyboard suited for programming (that is one that is completely reprogrammable by itself). I use a Kinesis, it's a little bit of money but they are extremely durable and quite nice.
Personally i have setup caps lock to behave like command on the system preferences and then on my emacs init.el file have:
(setq mac-command-modifier 'ctrl)
and this lets me use caps lock as command in most osx applications and as control in emacs. works well enough for me.
Your joy is just beginning. Other tricks include:
Use the left and right shift keys to also be ( and ) for fast typing.
Remap your fn key or another key to be "super".
Make caps lock be control, but only with another key. By itself, it is escape.
Read the excellent article at http://stevelosh.com/blog/2012/10/a-modern-space-cadet/ for a lot more information.
This thread was started 5 years ago and there is no mention of ns-win.el or build --with-ns. Here are all the key bindings available (out of the box) in Emacs Trunk as of October 2013. And, of course, you can create your own. Personally, I have one init.el with all my key bindings that can be used with Windows XP through Parallels on OSX, and also with OSX natively. Since the user can define his / her own keyboard shortcuts, I do not see a need to remap the keyboard in system preferences (with an Apple U.S. keyboard) unless using a keyboard that does not include the Command key. But, would I throw away my stash of IBM clicky keyboards with trackpoint built in? Of course not. :) I'm taking my IBM clicky keyboards with me into the next life. Any hand strain is most likely caused by improper wrist / arm / elbow position, not by hitting control versus caps lock. Accuracy is improved through practice, and with the help of a boss looking over your shoulder to see how you are coming along -- i.e., a little pressure to be more productive :)
(define-key global-map [?\s-,] 'customize)
(define-key global-map [?\s-'] 'next-multiframe-window)
(define-key global-map [?\s-`] 'other-frame)
(define-key global-map [?\s-~] 'ns-prev-frame)
(define-key global-map [?\s--] 'center-line)
(define-key global-map [?\s-:] 'ispell)
(define-key global-map [?\s-?] 'info)
(define-key global-map [?\s-^] 'kill-some-buffers)
(define-key global-map [?\s-&] 'kill-this-buffer)
(define-key global-map [?\s-C] 'ns-popup-color-panel)
(define-key global-map [?\s-D] 'dired)
(define-key global-map [?\s-E] 'edit-abbrevs)
(define-key global-map [?\s-L] 'shell-command)
(define-key global-map [?\s-M] 'manual-entry)
(define-key global-map [?\s-S] 'ns-write-file-using-panel)
(define-key global-map [?\s-a] 'mark-whole-buffer)
(define-key global-map [?\s-c] 'ns-copy-including-secondary)
(define-key global-map [?\s-d] 'isearch-repeat-backward)
(define-key global-map [?\s-e] 'isearch-yank-kill)
(define-key global-map [?\s-f] 'isearch-forward)
(define-key global-map [?\s-g] 'isearch-repeat-forward)
(define-key global-map [?\s-h] 'ns-do-hide-emacs)
(define-key global-map [?\s-H] 'ns-do-hide-others)
(define-key global-map [?\s-j] 'exchange-point-and-mark)
(define-key global-map [?\s-k] 'kill-this-buffer)
(define-key global-map [?\s-l] 'goto-line)
(define-key global-map [?\s-m] 'iconify-frame)
(define-key global-map [?\s-n] 'make-frame)
(define-key global-map [?\s-o] 'ns-open-file-using-panel)
(define-key global-map [?\s-p] 'ns-print-buffer)
(define-key global-map [?\s-q] 'save-buffers-kill-emacs)
(define-key global-map [?\s-s] 'save-buffer)
(define-key global-map [?\s-t] 'ns-popup-font-panel)
(define-key global-map [?\s-u] 'revert-buffer)
(define-key global-map [?\s-v] 'yank)
(define-key global-map [?\s-w] 'delete-frame)
(define-key global-map [?\s-x] 'kill-region)
(define-key global-map [?\s-y] 'ns-paste-secondary)
(define-key global-map [?\s-z] 'undo)
(define-key global-map [?\s-|] 'shell-command-on-region)
(define-key global-map [s-kp-bar] 'shell-command-on-region)
;; (as in Terminal.app)
(define-key global-map [s-right] 'ns-next-frame)
(define-key global-map [s-left] 'ns-prev-frame)
(define-key global-map [home] 'beginning-of-buffer)
(define-key global-map [end] 'end-of-buffer)
(define-key global-map [kp-home] 'beginning-of-buffer)
(define-key global-map [kp-end] 'end-of-buffer)
(define-key global-map [kp-prior] 'scroll-down-command)
(define-key global-map [kp-next] 'scroll-up-command)
;; Allow shift-clicks to work similarly to under Nextstep.
(define-key global-map [S-mouse-1] 'mouse-save-then-kill)
(global-unset-key [S-down-mouse-1])
(not an ergonomic keyboard, but i really like the keys' travel and feel, and Control key , Caps Lock are swapped).
http://matias.ca/osxkeyboard/index.php
I've created a fairly comprehensive set of bindings here for use outside of Terminal.
Personally, I can't use the mac laptop keyboard due to the absence of the right control key.
Instead, I have been using the Microsoft Natural Ergonomic Keyboard 4000 for over 7 years: it's got really fat, well-positioned Ctrl and Alt keys, and after downloading its drivers the "Start" and "Application" keys are trivially remapped to the Mac Cmd key, which is also fat and easily depressed.
To avoid emacs ergonomic concerns I've trained myself to use Ctrl, Alt, and Cmd the same way we use Shift - depressing them with the hand opposite the one typing the actual key. (That is, I just leave Caps Lock as is.)
I set caps lock to control in system preference and I have the following in my init.el to set command to meta and option to super:
(custom-set-variables
'(ns-alternate-modifier (quote super))
'(ns-command-modifier (quote meta)))
I would like to recommend 2 softwares
Seil and Karabiner. By simply installing Seil and following the instructions in the software, you should be able to achieve what you want. From my experience, Karabiner is more powerful. I have a Japanese keyboard whose layout is different from the normal ones. I have some snippet which remaps two extra keys on my keyboard to F18 and F19 for other use. You can use the same syntax to edit your "private.xml" file to do more things.

Resources