2

Sometimes I have application, which supports mouse (for ex. Midnight Commander) crash or disconnect but after that terminal remains in some mode, which makes it accept mouse movements as string commands:

(base) dimskraft@studebaker:~$ set mouse=
(base) dimskraft@studebaker:~$ set ttymouse=
(base) dimskraft@studebaker:~$ 0;32;19M32;33;19M32;34;19M32;36;19M32;37;19M32;38;20M32;39;20M32;41;20M32;42;20M32;43;20M32;44;20M32

How to disable it once it already happened?

I observe it in Terminator and can see different behavior in different panes. As you see, setting mouse and ttymouse to nothing doesn't help.

Dims
  • 3,255

2 Answers2

4

Those settings apply to Vim, not to the shell.

I think the best approach is to reset the terminal:

reset

This disables mouse tracking, and will also undo other changes made by tools such as Midnight Commander (try pressing CtrlC after MC crashes for example).

Alternatively, you could determine which mouse tracking mode is in use, and disable that explicitly (e.g. echo -e '\e[?1002;1005l' after Midnight Commander crashes in my terminal), but that will still leave other settings as-is. stty sane will restore sanity to the line discipline which will take care of those.

Stephen Kitt
  • 434,908
1

reset is vast overkill. For starters, simply re-starting and then properly exiting the application will fix this. It after all turns off the mouse reports that it has asked for at exit.

Alternatively, you just yourself send to the terminal the control sequences that turn the mouse reports back off. You can do this with the printf shell utility, entering the control sequences by hand using the escape notation.

For convenience, and for user-space virtual terminals, I created an improved portable version of the setterm utility that can do this. It works with GUI terminal emulators and the BSDs, unlike the util-linux one; and it is sensitive to the fact that only some terminal families properly handle the control sequences:

% printenv TERM
dumb
% setterm -7 --xterm-mouse-reports off --dec-locator-reports off
% setterm -7 --xterm-mouse-reports off --dec-locator-reports off | console-decode-ecma48
% setterm -7 --xterm-mouse-reports off --dec-locator-reports off | hexdump -C          
% export TERM=xterm-256color
% export XTERM_VERSION=999
% setterm -7 --xterm-mouse-reports off --dec-locator-reports off | console-decode-ecma48
DECELR 0
DECSLE 0
DECRM 1006
DECRM 1003
DECRM 1002
DECRM 1000
% setterm -7 --xterm-mouse-reports off --dec-locator-reports off | hexdump -C
00000000  1b 5b 30 27 7a 1b 5b 30  27 7b 1b 5b 3f 31 30 30  |.[0'z.[0'{.[?100|
00000010  36 6c 1b 5b 3f 31 30 30  33 6c 1b 5b 3f 31 30 30  |6l.[?1003l.[?100|
00000020  32 6c 1b 5b 3f 31 30 30  30 6c                    |2l.[?1000l|
0000002a
%

There are the control sequences.

Further reading

JdeBP
  • 68,745
  • 1
    reset is overkill in the sense that it does more than necessary, even including side effects (like erasing the scrollback). But it's a great choice in the sense that it's a solution for many similar problems, and is easy to remember. In this sense downloading, compiling, installing a custom software is definitely an extremely inconvenient solution; even if it's already installed, having to remember or look up command line flags such as --xterm-mouse-reports off might be too much if you suddenly need to fix the terminal. Anyway, it's great to see multiple approaches to choose from! – egmont Feb 19 '20 at 18:40
  • It's not a great choice at all. Indeed, quite often, largely because people wrongly promote it as a great choice for all ills and a magic incantation, isn't even a correct choice. https://superuser.com/a/1259560/38062 https://unix.stackexchange.com/a/494685/5132 – JdeBP Feb 20 '20 at 02:01
  • 1
    "It's not a great choice at all." – It works in many cases, including this one. It's easy to remember. It's quick to type it. It gets the job done in 2 seconds, without getting in your way, without having to switch mental context in order to recall something complex, look up a manual, or even download and compile stuff; so that you can immediately get back to what you were doing in the first place. Don't you think these are also important factors in judging whether a tool is great or not? – egmont Feb 20 '20 at 08:30
  • Quoting your own answer: “reset addresses situations when it is actually the terminal's own state that needs resetting and not (just) the state of the line discipline; i.e. the tabstops have been erased, the terminal is left switched to the alternative screen buffer, an inconvenient 8-bit character set has been swapped in, and so forth.” That’s exactly the situation here. Restarting the crashed program won’t help in situations where it always crashes for whatever reason. – Stephen Kitt Feb 20 '20 at 08:41