4

I have asked a question about Ctrl-arrow keybinding in Emacs in terminal:

Emacs Ctrl modifiers don't work in console

And was told, that Linux terminal emulator doesn't process this combination. I managed to create a file for loadkeys command, that processes these keys:

control keycode 105 = F100
string F100 = "\033[[left"
control keycode 106 = F101
string F101 = "\033[[right"

Then loaded it from root:

#loadkeys ./funcskeys

After that every time I click Ctrl-right or Ctrl-left in console, I get 'right' or 'left' printed. Now I need to process this in Emacs. As far as I understand from this question:

Binding M-<up> / M-<down> in Emacs 23.1.1

it must be done, using input-decode-map function. But I couldn't make it work. Plz, help.

Community
  • 1
  • 1
user4035
  • 22,508
  • 11
  • 59
  • 94

1 Answers1

7

Change your "funcskeys" file slightly to produce the following escape sequences:

control keycode 105 = F100
string F100 = "\033[1;5D"
control keycode 106 = F101
string F101 = "\033[1;5C"

Then add the following lines to your .emacs file:

(define-key input-decode-map "\e[1;5C" [(control right)])
(define-key input-decode-map "\e[1;5D" [(control left)])

After running loadkeys and restarting Emacs, CTRL+left and CTRL+right should work. You can verify this by typing:

C-h k C-right

and

C-h k C-left

To actually bind these keystrokes to a command, such as forward-word, you might have to add the following lines to your .emacs file as well:

(global-set-key [(control right)] 'forward-word)
(global-set-key [(control left)] 'backward-word)

Note that this whole approach specifically only makes the key combinations CTRL+left and CTRL+right work. It does not for instance make ALT+left / ALT+right work, or any other key combinations involving the CTRL character.

Thomas
  • 17,016
  • 4
  • 46
  • 70
  • I am not sure why, but your approach didn't work. I made it with (global-set-key "\M-[1;5C" 'forward-word). Then your elisp code is not necessary. Do you know, what's wrong? – user4035 Nov 06 '12 at 15:40
  • Sorry, but "didn't work" alone is not enough information to have an idea what's wrong. But try the variation I just made. – Thomas Nov 06 '12 at 21:36
  • Great! Regarding the other keys: if you have access to some other terminal emulation, like e.g., gnome-terminal, it would make sense to use the same key escape sequences. See my answer to your previous question to find out how to get these sequences. – Thomas Nov 07 '12 at 21:56
  • I added a detailed algorithm to the previous question for the people, who will face the same problem. – user4035 Nov 07 '12 at 22:03