7

I have bound M-[ to backward-paragraph. This causes a problem in terminal mode, whereby every time the mouse enters or leaves the frame (i.e., Emacs gains or loses focus), the characters I and O are inserted in the buffer.

To reproduce:

start vanilla emacs in terminal mode:

emacs -nw -Q

bind a function to M-[:

(global-set-key "\M-[" 'backward-paragraph)

move the mouse in and out of the Emacs frame.

What I see are the letters I and O inserted in the window. view-lossage shows:

 ESC [                  ;; backward-paragraph
 O                      ;; self-insert-command
 ESC [                  ;; backward-paragraph
 I                      ;; self-insert-command
 ESC [                  ;; backward-paragraph
 O                      ;; self-insert-command
 ESC [                  ;; backward-paragraph
 I                      ;; self-insert-command
 ESC [                  ;; backward-paragraph
 O                      ;; self-insert-command
 ESC [                  ;; backward-paragraph
 I                      ;; self-insert-command

I can still use M-[ as a keybinding - pressing it causes point to move backward as expected.

Is this a normal deficiency of terminal-mode emacs, or is it a bug?

Emacs version 27.0.50
Debian with i3 window manager
confirmed with gnome-terminal and xterm

Tyler
  • 21,719
  • 1
  • 52
  • 92

1 Answers1

5

This is a normal deficiency of most terminals. Terminal send escape sequences to represent input events that don't have a corresponding character, such as function keys, “exotic” modifier combinations and mouse events. Most escape sequences start with ESC [. Additionally, the meta modifier, for ordinary characters, is encoded as the ESC character followed by the base character. So when you press Meta+[, Emacs sees ESC [; when you press e.g. Left, Emacs sees either ESC O D or ESC [ D depending on the terminal configuration; and your terminal seems to send ESC [ I and ESC [ O for mouse enter/leave events.

A few terminals, including xterm, can be configured to use a different, non-backward-compatible encoding of input events that is not ambiguous. See Problems with keybindings when using terminal for more information including how to set it up in Emacs.

But for most terminals, including gnome-terminal, you're stuck with Esc being effectively unusable except as a prefix that meta-ifies the next key, and Meta+[ being effectively unusable, and Ctrl+[ being undistinguishable from Esc, etc.

  • Ok. I was aware of the general problem with escape keys in terminals, but thrown off by the unexpected interaction with the mouse. The keybinding itself works just fine - pressing `M-[` calls `backward paragraph` without any problem. – Tyler Apr 03 '19 at 15:36