Disable mouse reporting in a terminal session after tmux exits unexpectedly

iterm2sshterminaltmux

I currently use iTerm2 and frequently SSH into remote servers and start a tmux session. On all of those servers, when SSH'ing into them, I automatically create a tmux config that enables mouse reporting with the following in ~/.tmux.conf:setw -g mode-mouse on

However, if my SSH session ends abruptly and tmux is thus not given the chance to disable mouse reporting, using the mouse anywhere in the terminal windows introduces strange codes such as:

$ 0;94;18M0;94;18m0;19;33M0;14;33m

I see that I am not the only one experiencing this issue, see https://code.google.com/p/iterm2/issues/detail?can=2&q=&colspec=ID%20Type%20Status%20Priority%20Milestone%20Owner%20Summary&groupby=&sort=&id=855

I believe an easy solution for this problem would be to create a trap in a bash script that is used to open my SSH sessions. The trap could then disable mouse reporting whenever the SSH to tmux session ends.

The problem is that I have no idea how disable mouse reporting from within bash. I have found an article describing the console codes however I have not had any luck with this.

How can I send a command to the terminal to disable mouse reporting?

Best Answer

To send codes to the terminal from bash you can use the printf command. (You can also use echo, of course, but printf is generally better suited to sending terminal commands, since it has explicit support for sending ESC and makes it easier to separate the command characters from parameters, etc.)

Tmux, as well as most popular programs that support terminal mouse tracking, use XTerm Mouse Reporting (aka “X11 Mouse Reporting”).

To disable Mouse Reporting from bash, use this code:

printf '\e[?1000l'

ESC [ ? … l means “DEC Private Mode Reset (DECRST)” (the same code ending with an h instead of an l enables the feature).

There is also an older “X10 Mouse Reporting” protocol, which uses 9 instead of 1000, that can be disabled with ESC [ ? 9 l, but as far as I am aware you’re not likely to see this feature used for mouse tracking.

I normally use the Xterm Control Sequences documentation as my canonical reference for terminal codes and I recommend it, since it is both a practical guide to what XTerm supports and it is also the most comprehensive documentation of commonly supported codes that I've found.

Related Question