Process "emacs" alt-shift combinations in Iterm2 on Mac OS - macos

I tried many manuals how to deal with alt key in emacs in iTerm, but no of them helped me. The problem is that all suggestions work for alt key, but not for combination of alt+shift.
What do I want:
(global-set-key (kbd "M-S-<left>") 'shrink-window-horizontally)
(global-set-key (kbd "M-S-<right>") 'enlarge-window-horizontally)
(global-set-key (kbd "M-S-<down>") 'shrink-window
(global-set-key (kbd "M-S-<up>") 'enlarge-window)
So, alt+shift+arrow must change the window size. I can easily support this for alt+arrow using this manual:
https://azaleasays.com/2013/07/05/setting-up-mac-os-x-and-iterm2-for-emacs/.
But all this doesn't work for alt+shift.
Also I tried these suggestions. It doesn't work in this particular case:
Making iTerm to translate 'meta-key' in the same way as in other OSes
I don't think it's important, but for pure picture I connect via SSH to instance and work under tmux.
Did anybody try to do this?

Okay, I mostly fixed it.
The main problem is that I couldn't find any description of how to correctly write "Key Mappings" for Iterm2 (googled a lot). After some attempts I could somehow resolve this as a puzzle, but it's really strange to do it like this not having any good documentation. If somebody has a link, please share it.
For people who potentially have this problem I'll write what I could find:
Find the actual code of the key using "showkey -a" command. It usually has 2 values, let's name them X and Y. In this case the code "1A" (arrow up) would have X = 1, Y = A.
Use the following list as reference. I found the codes only by entering numbers and check. No documentation found on this. You can try another numbers if you need another key combinations:
SHIFT – 2
ALT – 3
ALT+SHIFT – 4
CTRL – 5
CTRL+SHIFT – 6
CTRL+ALT – 7
Let's name the previous value with variable Z.
The format for the "Key Mappings" is "[X;ZY" in this case. So, we split the source code to 2 parts, put "[" before and ";Z" inside.
So, CTRL+SHIFT+"Arrow up" would be "[1;6A".
It's not full though. For example, I couldn't understand how to code letters with ALT. For example, CTRL+ALT+z. If somebody knows, please comment also.

Related

Emacs 25 on Windows does not recognize some keybindings

I ignored Emacs 25 because of this for a while now and used Emacs 24 but now I have to switch...
So here is the problem: I have a different keyboard layout than default (Colemak) and the key combination C-M-b (for example) inserts some unicode character (˘). Emacs 24 ignored this and executed backward-sexp as I wanted it. In Emacs 25 though the key is taken literally and inserts the character. I did not find a way to overwrite it.
I tried (global-set-key (kbd "C-M-b") 'backward-sexp) and (global-unset-key (kbd "C-M-b")) but neither had any effect.
If I run C-h k C-M-b (the describe-key function) it waits for more input, as if C-M-b was a prefix to some key.
Using (global-set-key (kbd "˘") 'backward-sexp) (which is ugly anyways) does not work properly either, because the ˘ character gets inserted only after hitting that combination twice, then it gets inserted twice, tough (as for all accent marks). This results in the command running twice and only if i press the key combination twice.
EDIT: This issue occures on Windows 10, on my Linux machine with the same keyboard layout it is not an issue.
How can I fix this? It's truely annoying. Thx in advance
I found out, how to fix this issue. Turns out that my keyboard layout had a so called "dead" key at that very key combination. I do not know why Emacs 24 did'nt struggle with that while Emacs 25 did, but the solution for me was to modify my keyboard layout and make that key non-dead.

How can I remap TAB in Emacs?

This feels like it should be simple but it's been driving me crazy. I've got a function indent-or-expand that I'd like to remap to tab but I simply can't get it to work (Emacs v24, OS X). The only help I've been able to get from Emacs itself is:
error "To bind the key TAB, use \"\\t\", not [TAB]"
Doing (global-set-key [\"\\t\"] 'indent-or-expand) binds the function to <"\t"> apparently (whatever that is), and every combination I've tried of \, ", [], and () has failed.
I DID manage to bind the function to t, though...
In addition to what others have told you:
The Emacs error message you cite told you to use "\t", and if you use that you should be OK:
(global-set-key "\t" 'indent-or-expand)
Be aware also that TAB is one thing and <tab> might be another thing. IOW, it depends what code your physical keyboard Tab key actually sends to Emacs. TAB is the tab character, and it i the same as ACSCII control character C-i, that is, Control + i, which has decimal integer value 9. <tab> is (in Emacs) a pseudo function key. (Most likely TAB is what you want. Use C-h k to see what your physical Tab key does.)
Use the kbd function, i.e.:
(global-set-key (kbd "TAB") ...)

Carbon Emacs: no `focus' in minibuffer on incremental search

Good day all,
my trusty old Carbon Emacs has developed a frustrating habit: when I use C-s, the minibuffer prompts for a search string, but it is not focussed. Obviously, typing in a string doesn’t do anything. I have no clue which buffer has the focus at that time.
When I press C-g, the search is cancelled as usual. When I press Return, the I-search prompt turns into a Search prompt, and the minibuffer regains focus. Update --- When I type C-w, the current word is set as search string, as usual, and subsequent C-s advance the search as expected.
AFAIK, I have not changed anything in the .emacs file, not customized anything. The version of emacs is the same, and the OS as well. This behavior also survives a restart of emacs. Even emacs -q doesn’t solve it.
Update -- turns out that `focus' is not the right word; with i-search, the focus stays on the buffer you're searching in, but the mini-buffer echos the search term. Well, it should, but it doesn't. So, the following snippet is looking in the wrong direction.
I’ve tried this snippet to regain focus on the minibuffer:
(defun minibuffer-refocus ()
"Refocus the minibuffer if it is waiting for input."
(interactive)
(when (active-minibuffer-window)
(message "")
(select-window (minibuffer-window))))
(global-set-key (kbd "C-M-g") 'minibuffer-refocus)
But to no avail. It works like a charm in other cases, but not for the incremental search... Update -- That was to be expected.
Is there anyone that has any idea what’s going on, or has tips to hunt it down? This is really annoying...
Details
GNU Emacs 22.3.1 (i386-apple-darwin9.5.0, Carbon Version 1.6.0) of 2008-11-01 on leopard.local
OSX 10.6.7
C-s runs the command isearch-forward
Update
the good news is that is is solved. The bad news is that I don't know how :-(
Update
The bad news is that it is back again. I also realize that the above snippet does really help because the mini-buffer doesn't really get focus.
Note that when you ran C-s, M-e provides a means to edit the searched pattern, but it is much less efficient than having C-s work as expected...
I face the exact same issue (on two different Lion machines), and I do not understand what is going wrong, yet I can tell you that:
I use several "Input methods", i.e., several keyboard mappings: French and US Extended. I have selected the OS X option that allows to have keyboard mappings that depend upon the current application (I typically use US for Emacs, and French for mails for instance).
The French mapping works properly,
The US extended mapping fails as you described it,
In the same Emacs session, going from on to the other preserves that invariant: French works, US Extended is broken,
The good news is: US not-extended seems to work properly! Maybe (maybe) C-s is some prefix on OS X in the US extended mapping for some multi-key character, and some very bad interaction are started between OS X, maybe expecting for the next character part of the multi-key sequence, and Emacs which steals them from it. Wild guess.
The good news is that it works again as expected. The bad news is that I don't know what fixed it.
I do think that #vpit3833 was pointing in the right direction with the recursive edit suggestion; I was answering another question about the Alt and Esc keys and after trying out some things, like doing ESC-x and similar combo's, the incremental search started working again.
Sorry I have no real answer, but at least I can close this topic.
This is solution that work for me. I post it because I search for solution and couldn't find it.
(defun isearch (arg)
(interactive "MI-search: ")
(select-window (minibuffer-window))
(isearch-forward))
(global-set-key "\C-s" 'isearch)
I'm using GNU Emacs on Ubuntu.
EDIT: but if you use it you will not be able to search minibufer. Because of this feature you have no focus in minibufer when you search. For example you can do something like M-x C-s to search for functions names. And if you have focus in minibufer you can't do that.
I hit exactly the same problem, quite out of the blue, and discovered (as someone already pointed out) that it is something to do with OS X's own keyboard/input methods. I had been using the regular US keyboard, but then changed to the Unicode keyboard, which is when the problem arose. Once I went back to the regular US keyboard, i-search worked as expected. I got the same anomalous behavior with the US-extended keyboard.

What does "s-[keyname]" refer to in Emacs, and how do I tell Emacs to ignore it?

Background information:
I'm on a Mac, and I've just upgraded to Emacs 23.1 via http://emacsformacosx.com/. There are a few issues, notably the lack of full screen ability.
I've attempted to get around this last issue by installing Megazoomer, which adds a global input manager bound to Cmd-return. This causes the currently forward application to maximise. However, Emacs reports that <s-return> is undefined. I've never seen an s-[key] mentioned before, and Google isn't forthcoming with an answer.
So, two parts:
What does s-[key] mean? This is purely for my satisfaction; and
Can I tell Emacs to ignore this key combination and let the key combination carry through to the system (so that hopefully I can have full screen Emacs back again)?
EDIT: so 1) is resolved, and as to 2) I've got: (global-set-key (kbd "<s-return>") 'ignore), which at least stops the error. However, Emacs still swallows the key combination, which isn't ideal.
It's the Super key, like M- is the Meta key (alt key on a PC keyboard, Command key on your keyboard) and C- is the Control key.
I have of course never actually seen a super key on my keyboard... they are from a long gone era. Wikipedia has an image of this impressive "Space Cadet keyboard" which has all the modifiers you'll ever need:
With plain Emacs 23.1 on a Macbook Pro, I can map the right option key to super by
(setq ns-right-option-modifier 'super)
Your other choice seems to be the function key, which would be ns-function-modifier. However, fn might have other uses, whereas Emacs’ default is to map ns-right-option-modifier to ’left (ie, the same effect as the left option key, which I at any rate need to get the # character!), so the right option key is to some extent redundant.
Left-handers may want to reverse this.
For the question about what the s-[key] means, on ubuntu box it means the Windows® shaped key. What it means on the OSX systems, I do not know.
As for maximizing windows, could you try this?
(It should work, iif OSX runs an X server somewhere underneath it all)
(if (equal (window-system) 'x)
(progn
(defun toggle-fullscreen ()
"Toggles fullscreen"
(interactive)
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
'(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
(x-send-client-message nil 0 nil "_NET_WM_STATE" 32
'(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)))
(global-set-key (kbd "C-c C-y") 'x-clipboard-yank)
(global-set-key (kbd "M-RET") 'toggle-fullscreen)))
This little snippet is what I use to toggle fullscreen on my *nix computers. And yanking from X's clipboard is a neat ability to have.
As for how to set keybindings, use global-set-key for mode independent keybindings.
(Add it to your .emacs file if you want it to be permanent.)
(setq ns-command-modifier nil)
That is supposed to do what you want. However, it's having somewhat unpredictable behaviour on machine when I test it, so be warned.

OS X Terminal: Meta key + alt functionality at the same time

Is there a way to use an alt / option key as a meta key but still be able to use it to make some characters which need it?
For example, in my local keyboard layout:
# is alt + 2
\ is alt + shift + 7
| is alt + 7
etc.
So, if I set alt as a meta key, I can't make those characters anymore. On the other hand, using "press esc, release esc, press a key" to make meta key sequences makes my hands hurt.
Any Emacs users with international keyboards who have solved this, please give any tips you might have! :)
Edit:
It appears that I can set alt as a meta key and then add these kind of settings in inputrc: "\e2": "#" This works in the bash shell but it still won't work with Emacs though, so no good.
I use a Swedish keyboard in Mac OS X and use the following setup for Emacs.app:
First I have KeyRemap4MacBook installed and I have it setup to change the left Option key to Command.
I then have the following in my .emacs:
(setq mac-option-modifier 'none)
(setq mac-command-modifier 'meta)
This makes Emacs treat command as Meta and ignore Option. Since the left Alt/Option key is remapped to command, this makes it work lite meta while the right Alt/Option key still works for entering special characters like # [] and {}.
App specific Mac shortcuts like Cmd+C and Cmd+V no longer work, but global shortcuts like Cmd+space for Spotlight and Cmd+Tab for app switching do.
Update:
If you use iTerm2 as a Terminal, that has support for mapping only the left Alt/Option key to meta, which means you can still use the right AltGr/Option key for entering special characters. This also means you can use meta as normal inside Emacs running in the iTerm2 terminal.
I have a Swedish keyboard on my Mac and I'm using Aquamacs. Adding the two lines (attribution goes to Joakim Hårsman)
(setq mac-option-modifier 'none)
(setq mac-command-modifier 'meta)
in .emacs (and restarting Aquamacs) did the trick for me.
with iTerm2 you can map the left option key to ESC/meta and leave the mapping of the right option key to "normal". This way you can use the right option key to type accents etc.
Think about using a full featured Emacs for Mac OS X. I use Carbon Emacs but I believe others (say Aqua Emacs or X11 Emacs) would do the trick.
If you insist on using Emacs in a terminal you can use the C-x 8 prefix as a compose key. Try C-x 8 C-h to get a list of possibilities.
See http://www.emacswiki.org/emacs/EmacsForMacOS
Two work-arounds I use:
I use xterm running under X11.app. X11.app uses the command key as meta.
When running emacs in Terminal.app, I just use the rfc1345 input-method, which allows me to enter all kinds of crazy non-ascii characters without needing an option key.
Check out cmd-key-happy. This little app has worked wonders for me. It allows you to use command as meta, with exceptions that you can configure.
Author's info follows:
This program allows you to swap the command and alt (or option) keys
in any application, but in particular Terminal.app.
http://github.com/aim-stuff/cmd-key-happy
This is an old post but adding an alternatives for Robots to find.
I borrowed a couple of files from Aquamacs(emulate-mac-keyboard-mode.el, aquamacs-tools.el) that fixes the problem (I have a Italian-Pro keyboard) :
(defun aq-binding (any)
nil)
(load "~/.emacs.d/emulate-mac-keyboard-mode.el") ;; 'noerror
this provides:
few minor modes (emulate-mac-italian-keyboard-mode,
emulate-mac-french-keyboard-mode,
mulate-mac-swiss-german-keyboard-mode, etc)
a menu to play with all the options provided by Aquamacs
Tested with Emacs-23.3-universal-10.6.6 and seems to work ok for me.
Thanks Aquamacs for this, what about pushing this upstream if not already done.
It's worth thinking about the assumption behind the question. I mean to say that the real problem is making the characters that currently seem to need the option key. This problem can be solved without achieving simultaneous 'meta key + alt functionality'.
Let me explain: I have a similar problem. On my UK keyboard, shift-3 is mapped to the pound sign, £, which means that I generally need to hit option-3 to get the very useful # - a real problem in Terminal, where I use option as meta. However, I find most of the above solutions to be a little over-the-top. I don't want to change to another version of Emacs (eg. Aquamacs), to switch to a different Terminal application (eg. iTerm or xterm), or to install a key-remapping application (eg. KeyRemap4MacBook) all for the sake of one symbol! Perhaps that would suit some people, but I thought it was worth pointing out that if the problem is just one character, it can be solved with a nine-character text file.
With regard to my personal version of this problem, I find the simplest solution is to create a file called .inputrc in my home directory, consisting of the following single line:
"§": "#"
This re-maps the (to me, quite useless) § that can be found to the left of the 1 on the current Mac keyboard to the (almost indispensable) #. That suits me even better than the default setup, because I can now get # with a single keypress. If there were any other characters I needed to remap, it would just be a matter of adding an extra line for each one.
Anyway, that deals with how to get the missing character in the shell, but if you also want it in Emacs (which you didn't say but is probably the case) then you can add something like this to your .emacs file (where 35 is the ASCII for the character I happen to want, ie. #):
(global-set-key (kbd "§") 'insert-hash)
(defun insert-hash ()
(interactive)
(insert 35))
It might not be such a neat solution if there were dozens of characters that I routinely wanted to access via the option key, but there aren't.
I hope this helps somebody. As a mere weekend hacker, I ordinarily wouldn't dare post an answer to a question on Stack Overflow...
Special keyboard layout with option deadkey
My solution to this problem is a special keyboard layout I have written, U.S. custom. It provides an option deadkey that produces option combos without having to use the physical ⌥ Option modifier key. For instance, the character ⟨ç⟩ is produced as follows on a normal U.S. keyboard layout:
Hit ⌥ Option+c.
The U.S. custom keyboard layout adds a second way of producing ⟨ç⟩:
Hit ⇧ Shift+§ (the option deadkey), then release it, then hit c.
This works even in Terminal.app when Use option as meta key has been checked.
Note that the U.S. custom keyboard layout does two additional things:
It converts plain § into a ⎄ Compose key (so you could also produce ⟨ç⟩ by hitting §, then ,, then c).
It replaces the CapsLock mapping by a (significantly extended) U.S. Extended keyboard layout.
Remap option deadkey to right option modifier key
If you have a physical ANSI keyboard, then you lack the § key. In that case, I recommend assigning the § key to some other key, for instance to the right ⌥ Option modifier key. Install KeyRemap4MacBook, open ~/Library/Application\ Support/KeyRemap4MacBook/private.xml and paste the following code:
<?xml version="1.0"?>
<root>
<item>
<name>Send ISO Section for right Option</name>
<identifier>private.send_iso_section_for_right_option</identifier>
<autogen>--KeyToKey-- KeyCode::OPTION_R, KeyCode::UK_SECTION</autogen>
</item>
<item>
<name>Send Shift+ISO Section for Shift+right Option (keep normal right Option without Shift)</name>
<identifier>private.send_shift_iso_section_for_shift_right_option</identifier>
<autogen>--KeyToKey-- KeyCode::OPTION_R, ModifierFlag::SHIFT_R, KeyCode::UK_SECTION, ModifierFlag::SHIFT_L</autogen>
<autogen>--KeyToKey-- KeyCode::OPTION_R, ModifierFlag::SHIFT_L, KeyCode::UK_SECTION, ModifierFlag::SHIFT_L</autogen>
</item>
</root>
Then, open the KeyRemap4MacBook preference pane, hit ReloadXML and then select “Send ISO Section for right option”. Now, your ⌥ Right Option key is remapped to §, so while the U.S. custom keyboard layout is active, hitting ⇧ Shift+⌥ Right Option will give you the option deadkey state (hitting ⌥ Right Option without additional modifiers will give you a deadkey state equivalent to the option modifier.
If want to keep ⌥ Right Option when ⇧ Shift is not pressed (instead of the ⎄ Compose key), then select “Send Shift+ISO Section for Shift+right Option (keep normal right Option without Shift)” in the KeyRemap4MacBook preference pane (instead of “Send ISO Section for right Option”). Like that, only ⇧ Shift+⌥ Right Option will produce the option deadkey, while plain ⌥ Right Option will continue to work as an option modifier. You can even continue using ⌥ Right Option+⇧ Shift modifier combos (for instance ⌥ Right Option+⇧ Shift+c→⟨Ç⟩) as long as you don’t hit ⇧ Shift first.
The above answers mention ways to enter non-ascii input, but that's not really the problem here. The C-x 8 method lets you enter a bunch of non-ascii, but the problem is that you need the option key on mac just to enter the (ascii) character `|' (vertical bar)!
So how do you then enter stuff like M-| (region to shell command) when running emacs in ssh under Terminal.app? No way for it but to use the escape key, since | is option-7 and Terminal.app for some stupid reason won't let you use Cmc as meta :-(
(If anyone knows of a hack (SIMBL perhaps?) that actually does let Terminal.app use Cmd as meta, I'd be very grateful...)
The following solution works, is UNOBTRUSIVE and you don't need iTerm2 or Aquamacs or whatever.
Step 1
Keep "Use option as meta key" turned ON in the Terminal.app Settings.
Step 2
Edit ~/.inputrc (make it if it doesn't exist already), add the following line:
"\e3": '#'
This will remap ⌥ alt + 3 so that you produce a # character as expected in the terminal, instead of it annoyingly coming back with the prompt (arg: 3).
Step 3
Edit ~/.emacs, add the following line:
(global-set-key (kbd "M-3") "#")
This will remap ⌥ alt + 3 so that you produce a # character as expected inside emacs, instead of it annoyingly doing nothing.
Caveat 1, This is just 1 re-map. So ⌥ alt + ? (other keys) won't be remapped, e.g. you may wish to remap ⌥ alt + n to produce the tilde key ~. Simply follow the same steps outlined above obviously switching 3 with whichever key you need going forward.
Caveat 2, When you ssh into another box, you will face the same difficulties, but all you have to do there is edit the ~/.inputrc and ~/.emacs files on that box.
Caveat 3, I'm British, on a British MacBook. So I can't vouch for foreign MacBooks.
Aquamacs supports that, at least for my (french) keyboard. Once Aquamacs is installed (via drag'n'drop), you can go to Options -> Option, Command, Meta keys and choose an appropriate setting for your keyboard (in my case, ...Meta & French). I am not proficient with Emacs, but all the useful combinations I have tried seem to work ([, {, |, and so forth).
Supported keyboards, according to the menu items as of version 2.1, are :
British
US
Swiss-french
Swiss-german
Finnish
Italian-Pro
Italian
Spanish
French
German
I would guess the Aquamacs team would be grateful for any contribution, though, should your keyboard not appear in the list.
I bit the grass and started hitting the escape key instead. You get use to it...
I would like to mod this question up or something because i also use a Swedish keyboard where i must press alt+7 or alt+shift+7 to make | and \ respectively.
I also use Irssi in Terminal.app and these don't mix well.
I'm a long time vim user though so emacs is not an issue to me.
Isn't there some way to remap CMD to be Meta in Terminal.app?
So far i've been surviving by simply typing /wg N in Irssi because having those characters in my Terminal is much more important than IRC.
A solution would be worth gold to me though! :)
This is years late, but for people coming across this page whilst searching for a solution, as I did, I have decided to build one myself, one that doesn't require abandoning Terminal.app in favour of iTerm 2.
It is a simple status bar app that runs in the background and rewrites all left-Alt + $KEY key events to two key events in rapid succession, Esc, then $KEY; however, it only does this if Terminal.app is the focused application.
You can find it here at https://github.com/habibalamin/Metalt.
I assigned escape to the caps lock key and it works quite good. You can configure it easily in the mac system preferences > keyboard > modifier keys.
https://apple.stackexchange.com/a/282092/236382

Resources