1

I am connecting in a machine using a terminal, mosh and then screen. Then, in my orgmode file I can't use the shortcuts like SHIFT+up, SHIFT+left or CTRL+up etc. If I press that, it produces something like 2A instead the expected behavior. It leads onto accidental mistakes inserting undesired chars on a source code, for example.

I tried to insert the following lines into my .emacs config file but it did not work.

;; Fix SHIFT+arrows and CTRL+arrows on screen                                                                                                             
(define-key input-decode-map "^[2A" [S-up])
(define-key input-decode-map "^[2B" [S-down])
(define-key input-decode-map "^[2C" [S-right])
(define-key input-decode-map "^[2D" [S-left])
(define-key input-decode-map "^[5A" [C-up])
(define-key input-decode-map "^[5B" [C-down])
(define-key input-decode-map "^[5C" [C-right])
(define-key input-decode-map "^[5D" [C-left]))

Any ideas? Thank you!

  • If you hit C-left followed by C-h l what does Emacs tell you about the last few bytes it received? – Stefan May 18 '18 at 20:44
  • @Stefan pastebin.com/raw/yjrpgQK7 – Felipe May 24 '18 at 19:48
2

^[ is definitely wrong. It should be \e[ or \eO. ^[ is sometimes used to designate the escape character, which is also control-open-bracket, but there is no such syntax in an Emacs Lisp string literal. You need to use \e (or some more cumbersome syntax). Furthermore ESC 2 A is not a plausible escape sequence: most terminals use sequences that begin with either ESC O or ESC [.

To see what your terminal sends, press C-q C-left (which will insert a literal escape character, which you can use in a string literal but not copy-paste into a web browser), or press C-left f1 l to see how Emacs saw C-left (you can replace f1 by C-h).

For example, if C-left C-h l shows ESC [ 1 ; 5 D C-h l then you need

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

And chances are that your terminal uses the following encoding:

(define-key input-decode-map "\e[1;2A" [S-up])
(define-key input-decode-map "\e[1;2B" [S-down])
(define-key input-decode-map "\e[1;2C" [S-right])
(define-key input-decode-map "\e[1;2D" [S-left])
(define-key input-decode-map "\e[1;5A" [C-up])
(define-key input-decode-map "\e[1;5B" [C-down])
(define-key input-decode-map "\e[1;5C" [C-right])
(define-key input-decode-map "\e[1;5D" [C-left])
(define-key input-decode-map "\e[1;6A" [C-S-up])
(define-key input-decode-map "\e[1;6B" [C-S-down])
(define-key input-decode-map "\e[1;6C" [C-S-right])
(define-key input-decode-map "\e[1;6D" [C-S-left])
  • When I press C-q C-left I receive ^[5D. But ^[ is one char and it is in light blue. I tried to copy/past the result of C-q C-left, I tried \e[ and \e0 but both failed. What I could try more? – Felipe May 24 '18 at 19:56
  • @Felipe If you actually get ^[5D with the ^[ as a single character, you can write "\e5D" in a string literal, or copy-paste inside Emacs (don't copy-paste in a terminal emulator, that would copy the displayed representation with ^ and [ as separate characters). But it would be very surprising for a terminal to send ESC 5 D. It would be very common for a terminal to send ESC [ 5 D, which would show as ^[[5D with the ^[ as a single character. This is "\e[5D" in a string literal, or (kbd "ESC [ 5 D") if you like kbd. – Gilles 'SO- stop being evil' May 25 '18 at 8:50
  • I tried it: pastebin.com/raw/8QJNwhrB and it did not work yet! :-( What should I try more? – Felipe May 28 '18 at 23:43
  • @Felipe Once again: "\e[5D", not "\e5D". – Gilles 'SO- stop being evil' May 29 '18 at 7:52
  • 1
    @Felipe See my edited answer – Gilles 'SO- stop being evil' Jun 4 '18 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.