emacs in terminal meta arrow keybindings - emacs

I have just switched from using Emacs.app to emacs server and emacsclient in terminal mode using iterm2 as my terminal emulator. I am having some trouble with some keybindings though. Particularly M-left arrow prints the character D, M-right arrow prints C, M-up arrow prints A, and M-down arrow prints B. M-ret seems to work though, at least for org mode. I am using the xterm defaults for keys in iterm2 and have the left and right option keys bound to +Esc. I can get the M-left functionality in org-mode with Esc-left or Esc-right This is particularly annoying in org-mode. Am I going to have to just rebind the keys in my .emacs? How would I go about doing that?
I have looked at this http://orgmode.org/manual/TTY-keys.html#TTY-keys, but I don't understand why the arrow keys should be unavailable in the terminal.
edit:
Cat meta-up: ^[[1;9A
Cat meta-down: ^[[1;9B
Cat meta-right: ^[[1;9C
Cat meta-left: ^[[1;9D
Main problem solved, but I am now having trouble with shift-up. "<select> undefined". I tried a similar mapping with the escape sequence I got from cat: ^[[1;2A. Reluctant to create another question for a similar problem.

Solution 1
Based on the info you provided, here's one thing you can try. You tell emacs to map those escape sequences to the proper key sequences:
(add-hook 'term-setup-hook
(lambda ()
(define-key function-key-map "\e[1;9A" [M-up])
(define-key function-key-map "\e[1;9B" [M-down])
(define-key function-key-map "\e[1;9C" [M-right])
(define-key function-key-map "\e[1;9D" [M-left])))
Solution 2
I also found another possible solution with a little googling: redefine the iTerm bindings instead, to match what emacs is looking for.
http://offbytwo.com/2012/01/15/emacs-plus-paredit-under-terminal.html
Quote from the above page:
Go back to the profile key bindings under iTerm2 and add bindings for the following:
M-up : Esc-[1;4A
M-down : Esc-[1;4B
M-right : Esc-[1;4C
M-left : Esc-[1;4D

I'm answering in reply to your 'main problem solved, but new one' edit.
I found this guy's blog post on this issue:
- http://webframp.com/emacs/2013/02/22/fixing-emacs-bindings-on-the-in-iterm2/
Basically, you can use the 'run cat' and push buttons trick to see what escape codes are getting sent by your system/terminal, then add 'define-key' lines to define M-{up,down,right,left} and also M-S-{up,down,right,left}.

Related

shift up-arrow doesn't highlight text emacs iterm2

I recently had help fixing M-left and so forth here: emacs in terminal meta arrow keybindings, but am unable to fix Shift-up using a similar solution. When I try shift-up I get an error <select> is undefined. I've tried re-mapping it using:
(add-hook 'term-setup-hook
'(lambda ()
(define-key function-key-map "\e[1;9A" [M-up])
(define-key function-key-map "\e[1;9B" [M-down])
(define-key function-key-map "\e[1;9C" [M-right])
(define-key function-key-map "\e[1;2A" [S-up])
(define-key function-key-map "\e[1;9D" [M-left])))
But shift remains undefined. I also tried rebinding the key by setting it using the escape sequence returned from cat which is ^[[1;2A. Oddly enough shift down does work. shift-select-mode is marked at t as well.
This sounds like a trouble I had accessing a Ubuntu 12.04 machine via Putty, when END caused Emacs 23.3.1 to say <select> is undefined. That turned out to be an issue with the terminfo that lets programs use terminals in a device independent way.
Based on this 2008 bug report discussion, I solved my problem by adding the following to the top of my ~/.bashrc:
#so the END key will work correctly in Emacs over PuTTY
TERM=xterm-vt220
N.B., with either xterm-vt220 or the default xterm, emacs -Q -nw is getting ESC [ 4 ~ when I press END, ESC O A for Up, and ESC [ A for Shift-Up. (To see what keycodes Emacs is getting, press some buttons and then C-h,l.) For the same keys in the same order, cat says [4~, [A, and [OA...so Up and Shift-Up are oddly reversed.
If you don't want to change your terminfo, see this discussion for a workaround
http://lists.gnu.org/archive/html/help-gnu-emacs/2011-05/msg00211.html
You should be able to work around the issue with something like:
(define-key input-decode-map "\e[1;2A" [S-up])
And for this to take effect at the right time, you will have to use in your .emacs something like:
(if (equal "xterm" (tty-type))
(define-key input-decode-map "\e[1;2A" [S-up]))
Just to add more information about the solution:
https://github.com/arthurnn/dotfiles/blob/8d56f2419da9a4cb654d8941f379d6d5783bdb90/.emacs.d/init.d/setup-bindings.el#L3-L10 this should fix all the cases including emacsclient.
The last line is responsible for fixing the Shift-up when using emacsclient.

How to bind Emacs key for delete whole word backword as the same in a shell?

I have just started learning Linux and Emacs. It was pleasant to have the same key bindings in both Emacs and a shell (bash/tcsh) for the most frequently used cursor movements, so that I do not have to consciously think which one I have to use. Still worse, use the wrong command and undo the mistake. There were two exceptions, though.
One often used command was the equivalent of backspace, delete a character backwards. In a shell, it was C-h. I got the same behaviour in Emacs, thanks to this tip from Janos, who probably felt the same way.
http://www.math.rutgers.edu/~komlos/emacs.htm
Now the mistake I do often in Emacs is trying to delete words backwards with the command M-C-h, as in a shell.
Can somebody please give a binding that will make Emacs delete words backwards with 'M-C-h'? Currently, this command selects the whole of the text in a buffer, which is quite an useful thing (C-a in windows), but not so frequently used as deleting words backwards.
Moreover, any binding to replace the current binding of M-h (from the link above) to help will be appreciated.
Thank you,
Elan.
Below binds C-M h to backward-kill-word. You can put it in your .emacs file.
(global-set-key (kbd "C-M-h") 'backward-kill-word)
You can use M-<backspace> in terminal and emacs to delete word backward.
It's best to use key translation so C-M-h works exactly the same as M-backspace would in any minor mode (regardless of whether M-backspace is bound to backward-kill-word or not).
;; bind C-h to <backspace>
(define-key key-translation-map [?\C-h] [?\C-?])
;; bind C-M-h to M-<backspace>
(define-key key-translation-map [?\C-\M-h] [?\C-\M-?])

emacs - [control shift up] doesn't work

I tried to define hotkey as following
(global-set-key [(control shift up)] 'other-window)
but it doesn't work (no error, just doesn't work), neither does
(global-set-key [(control shift down)] 'other-window)
But
(global-set-key [(control shift right)] 'other-window)
and
(global-set-key [(control shift left)] 'other-window)
work!
But because the last two key combinations are used by emacs (as default), I don't wanna change them for other functions.
So how could I make control-shift-up and control-shift-down work?
I have googled "(control shift up)", it seems that control-shift-up is used by other people, (but not very few results).
The reason for this is not an Emacs problem, but comes from the fact that your terminal cannot produce a key sequence for C-S-up.
You can verify this very easily. Open a terminal and then type:
Control-v Control-Shift-right
The Control-v part will make the control sequence for the next key be inserted verbatim into your shell. In our case, it will insert the sequence for Control-Shift right, and that'll look something like this:
^[[1;6C
Now, try the same thing for C-S-up:
Control-v Control-Shift-up
You'll see that no control sequence is entered, which hints at the fact that when you press C-S-up in Emacs, it will actually never receive anything, because the terminal is not able to produce anything to pass on to Emacs.
We can double-verify this if you just start a new emacs -nw and type C-h k to invoke Emacs' describe-key function. You'll get asked in the minibuffer to type a key to see what function it is bound to. If you now type C-S-up nothing happens - of course not, since the terminal in which your Emacs runs doesn't produce anything.
However, if you're just looking for an unused key-combination, how about just Shift-up (or even Shift-right) without Control? That one should work both in a terminal emacs and in the windowed version.
Finally, with the help from grawity on superuser.com, I got it working. Please this thread
https://superuser.com/questions/230852/get-ubuntu-terminal-to-send-an-escape-sequence-controlshiftup
This could well be a duplicate of:
Binding M-<up> / M-<down> in emacs 23.1.1
If this is the case, Gilles' answer should sort you out (undoubtedly with different escape sequences, though.)
edit (for better visibility -- see answer below):
It turned out that gnome terminal does not transmit these key combinations, so the solution relies upon the following: https://superuser.com/questions/230852/get-ubuntu-terminal-to-send-an-escape-sequence-controlshiftup

Emacs define-key, Viper-mode key binding

I'm trying to learn emacs, getting vi custom key bindings.
Using Viper-mode, what is the correct way to re-bind a key? (I'm using Colemak keyboard layout(instead of qwerty) so have to change things like n->j) But would like it to work in viper-mode.
From this key binding guide on GNU.org:
http://www.gnu.org/software/emacs/manual/html_node/viper/Key-Bindings.html
It says the command to put in your .viper file is:
(define-key viper-vi-global-user-map "\C-v" 'scroll-down)
It doesn't work for me... in fact not sure I even have the function "define-key"...
M-x define-key [No match]
I'm not sure if 'define-key' is available on my version of emacs?
This works, but not in viper-mode
(global-set-key "n" "j")
Any help would be much appreciated. This is my first day using Emacs, to it's a pain getting Colemak & Viper-mode to work properly.
Thank for any help...
Hopefully some useful answers here:
First, having that line in the .viper works for me. Note that the viper-vi-global-user-map applies when you're in command mode, not insert mode.
Secondly, define-key isn't a command, it's a regular function, which just means that it cannot be called using M-x. See this Emacs wiki page for a little more detail on that distinction. But that was a good attempt.
Third, the global-set-key is a command, you could have tried making a change using M-x global-set-key. But, that sets the key in the current global map, which isn't the same as viper-vi-global-user-map. Viper-mode uses a bunch of different keymaps to make Emacs behave like vi, but all of the maps are overlaid on top of the global map.
I'm guessing that you found that C-v wasn't bound like you want when you're in insert mode. And that can be solved by adding this to your .viper:
(define-key viper-insert-global-user-map "\C-v" 'scroll-down)
Lastly, scroll-down may not be what you want. The down refers to the text moving down (given the perspective of a fixed window). C-v is generally bound to 'scroll-up. But,maybe it is exactly what you want.
Caveat: I'm not a viper-mode user, I don't even know how to use vi. So my terminology may be off. But I find the challenge of changing things in viper-mode very interesting.
Edited to add
From your comment it sounds like you want n to be the same as what j is bound to by default. Try adding this:
(define-key viper-vi-global-user-map "n" 'viper-next-line)
In "normal" mode I did M-x describe-key j, which told me that j is bound to 'viper-next-line, and the above line will bind n to the same routine. Repeat for the rest of the bindings you want to shift around.
in modern times evil-mode is the vim emulation layer for emacs, and to tweak it for colemak, my https://github.com/wbolster/evil-colemak-basics package helps a lot.

basic shortcut (M-del) not working in Emacs and giving erratic behavior

I am using Emacs and most shortcuts work normally, but M-Del for deleting a word backwards produces either an error at the bottom of a `scan' error, and at other times moves the cursor a set of lines below. Any ideas why this may be happening? M-Del works fine for deleting forward words. (** from a comment made below it appear that the command is mapped to a down paragraph lisp function instead of delete a word backwards? How can I reset the mappings to the standard one?)
Best.
writing lisp emacs key binding and cannot specify the <delete> character
has the answer (by Gilles). It looks like there is a bug on some systems due to an overlap with a shell command shadow translating ESC-x to ESC C-d
it can be seen from running
M-x load-library edmacro
M-x edmacro-insert-key M-del
giving ESC C-d
in the folder ~/.emacs.d/ creating a file init.el and inserting
(global-set-key [escape delete] 'backward-kill-word)
this though overrides all uses of ESC from M (meta key) to be translated as escape rendering common M-d, M-w, etc all unseen except for M-del
so the solution is to remap the faulty remapping back to the correct binding.
(global-set-key (kbd "M-C-d") 'backward-kill-word)
Best