Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"screen" somehow unmaps my arrow keys in emacs after a ^Z

Every time I use emacs, I can use the arrow keys just fine to move the cursor around. But when I'm running emacs within screen, and I push emacs to the background (ctrl-Z) then return it to the foreground, my arrow keys no longer work, e.g.

M-[ a is undefined

This behavior doesn't happen when I'm running emacs outside of screen.

Any ideas? Is this some screen setting?

like image 264
Aaron Fi Avatar asked Dec 28 '10 17:12

Aaron Fi


People also ask

How do I set arrow keys?

For smaller keyboards and many laptop keyboards, the arrow keys may be moved below the Enter and the right Shift . Also, some compact keyboards may move the arrow keys to the keypad and switch between the keypad and arrow keys by pressing the Num Lock key.

Does vim use arrow keys?

The vim editor is derived from vi. It also utilizes the same hjkl keys to replace the functionality of arrow keys.


2 Answers

For what it's worth, this just happened to me, and I was able to correct it by disconnecting from screen and then reconnecting:

Ctrl+A, d

screen -R
like image 71
Early Ehlinger Avatar answered Oct 25 '22 19:10

Early Ehlinger


The vt100 terminal that Screen (and just about every other terminal emulator) emulates has two modes, normal mode and application mode. Normal mode is used for line-by-line applications and application mode for full-screen applications. Amongst the differences between the modes is that the arrow keys send different control sequences (I don't know why): e.g. ESC O A in full screen mode and ESC [ A in normal mode. It seems that when you press Ctrl+Z, Emacs switches the terminal back to normal mode, but when you return it to the foreground, it doesn't switch to full screen mode again, or Screen does not react to Emacs's commands properly.

One possible workaround is to bind the same character sequences in both modes in your .screenrc, e.g.

bindkey -k ku stuff ^[OA
bindkey -k kd stuff ^[OB
bindkey -k kr stuff ^[OC
bindkey -k kl stuff ^[OD

Another possible workaround is to tell Emacs to interpret both key sequences regardless of what the terminal says. Although this is in principle a bad idea since some terminals might use these key sequences for different keys, I've never encountered such an incompatible terminal, and I suspect none have been made in the last 20 years or more.

(define-key function-key-map "\eOA" [up])
(define-key function-key-map "\e[A" [up])
(define-key function-key-map "\eOB" [down])
(define-key function-key-map "\e[B" [down])
(define-key function-key-map "\eOC" [right])
(define-key function-key-map "\e[C" [right])
(define-key function-key-map "\eOD" [left])
(define-key function-key-map "\e[D" [left])

A true solution would involve finding what's causing the problem. It could be a bug in Screen, a bug in Emacs, a bug in the terminal (emulator) that Screen is running in, a bug or misconfiguration in your termcap or terminfo database. If you want to investigate this, start by mentioning your operating system, what terminal (emulator)(s) Screen is running in, where you obtained Screen (or how you compiled it, if that's what you did) and what version, ditto for Emacs, whether you've observed the same problem outside Screen, the output of echo $TERM and echo -E "$TERMCAP" inside Screen.

like image 25
Gilles 'SO- stop being evil' Avatar answered Oct 25 '22 19:10

Gilles 'SO- stop being evil'