MacOS – Characters and notification bounce when Terminal loses focus


OSX 10.11.2
Terminal 2.6.1

I have an intermittent issue that is driving me up the wall. I spend a lot of time in Terminal in full-screen mode on my 2015 11" Macbook Air. I generally have Chrome or Notational Velocity or something else up in one of the "Spaces" nearby and switch back and forth with the four-finger-swipe gesture (enabled in System Preferences -> Trackpad -> Swipe between full screen apps).

Over the last several months, one of the tabs in my Terminal will sporadically start giving me a "bounce" and the red 1 badge to single some kind of alert any time it loses focus. I usually notice it when I'm using a four-finger swipe to go back and forth to another space (which immediately shows a single bounce of the Terminal icon and the badge in the dock).

The terminal acts normal upon return to focus. I don't see any notifications or message other than a few odd characters. It was hard to identify exactly what characters, so I ran cat and then changed focus, and it looks like it's ^[[O^[[I. I thought this kind of looked like some of the sequences to make arrow and special keys behave in Terminal -> Preferences -> Profiles -> Keyboard, but I don't see any with those characters.

I get the same problem when in normal (not full-screen) mode and click on a different icon — even in the same space — and Terminal loses focus. Same characters, a bounce, and badge, so I don't think it's related to Mission Control.

I am often running tmux and have SSH connections to a few servers, but I haven't noticed it being exclusive to a tmux or SSH session. For example, currently I have a few Terminal tabs open, and it's happening in a tab running an SSH session (not in tmux), but not happening in another tab running tmux in SSH, and not happening in other tabs open to local directories.

I don't see anything obvious in and nothing from Terminal. I usually try to include a "WHYT" section to my stackexchange posts, but I can hardly figure out where to start here — I'm stumped.

Not a major issue, but definitely annoying, and it shows no signs of leaving me alone over the last few months. Any help figuring this out would be greatly appreciated!

Best Answer

Some program you're interacting with has enabled terminal Focus In/Out events, normally used along with Mouse Reporting (aka “Xterm Mouse Tracking”). When enabled, the terminal sends events to the TTY to indicate when the terminal gains or loses keyboard focus (i.e., becomes active or inactive).

The terminal command for enabling focus events is:

CSI ? 1 0 0 4 h

(CSI = Control Sequence Introducer: ESC [.)

The focus in/out events that the terminal sends to the tty are:

Focus In:  CSI I
Focus Out: CSI O

These are the sequences you're seeing when you use cat to view them.

The terminal Dock-icon bounce and badge indicate when a BEL (Control-G) is sent to the terminal when it isn't the active/frontmost terminal. (There are preferences to control these behaviors in Preferences > Profiles > [profile] > Advanced.)

So it appears that some program has enabled Focus In/Out events and is sending a BEL in response to them.

It could also be that the program sending the BEL is not the same as the program that enabled the Focus In/Out events. For example, an editor like vim may have enabled them, but some intervening program like screen or tmux (or possibly ssh, although it seems less likely) is intercepting the Focus In/Out events and responding with a BEL to indicate that it considers the input to be invalid.

…it's happening in a tab running an SSH session…

Since you didn't say, I'll assume you're just using a shell within that session and not some other program. If that's the case, it's likely that you ran some preceding program within that SSH session that enabled the focus events and then exited without disabling them, leaving the shell to receive them, and if you type ESC [ I or ESC [ O manually into Bash, by default it will emit a BEL to indicate that the input is considered invalid.

If you have only used a shell within that SSH session, then you should examine the shell startup/configuration files on the remote host to see if there's some code to enable the focus events.

You can simulate this situation by running the following command in Bash (and probably other shells):

printf '\e[?1004h'

Then you'll find that Bash beeps (sends a BEL) when you activate or deactivate the terminal view/window.