8

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Zach
  • 996
  • 12
  • 25
  • Type `cat` on the command prompt and press the meta-arrow keys. This will show you the esc-sequences that your arrow keys output. Please add these to your question. For example on my terminal M-up outputs: `^[^[[A`. – Casper Jun 03 '12 at 01:31
  • ok done, this seems to comport with the output that I am getting in emacs when I try to use meta – Zach Jun 03 '12 at 01:37
  • 1
    Hmm..no, type `cat`, hit enter, and THEN press your arrow keys. – Casper Jun 03 '12 at 01:40
  • ah ok thanks, should be fixed now – Zach Jun 03 '12 at 01:44

2 Answers2

13

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
Casper
  • 33,403
  • 4
  • 84
  • 79
  • I had previously tried re-mapping it through iterm, but I must be doing something else wrong. The hook worked though. Thanks! – Zach Jun 03 '12 at 02:06
  • The lambda function doesn't need to be quoted. Quoting it creates a warning. – snath03 Jul 30 '22 at 03:35
2

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}.

jamessan
  • 41,569
  • 8
  • 85
  • 85
traviscj
  • 121
  • 1
  • 6
  • 1
    THANK YOU exactly what I needed. Seems like everyone's character codes are slightly different. Above used [1;4x]. That blog post showed [1;3x]. But mine were [1;9x]. Very confusing. – Chris Sep 26 '14 at 16:26