:help FocusLost says:
Only for the GUI version and a few console versions where this can be detected.
Poking around in the code revealed the function
ui_focus_change whose comment says:
Used for the GUI or for systems where this can be done in the console (Win32).
Based on searching for callers, that statement seems accurate (it is only automatically called (asynchronously) from the GUI code for various platforms and (synchronously) from the non-GUI Windows code).
An answer to a related question (“Can I make Tmux tell Vim when its pane loses focus?”) over on Unix & Linux pointed out a new Vim plugin called Vitality. The plugin makes a series of Vim configurations to enable FocusLost and FocusGained autocommands for instances running under iTerm 2:
- It configures Vim to send a special control sequences that cause iTerm 2 to send “focus out” and “focus in” control sequences when the iTerm 2 window loses/gains focus (though apparently iTerm 2 currently fumbles these sequences a bit when switching tabs inside a window).
- It configures Vim to recognize the “focus out” and “focus in” sequences as rarely used keys (F24 and F25).
This is done instead of just mapping the sequences so it is possible to enable
ttimeout and use a short
ttimeoutlen to avoid causing delays when pressing Escape by itself.
- It maps those keys to trigger FocusLost and FocusGained (via
Vitality is only designed to look for iTerm 2, but other terminal emulators also support the same “focus notification” control sequences:
- xterm from 10.6 supports them (probably xterm from most other platforms, too)
- Terminal from 10.6 does not support the sequences
- I do not know about 10.7’s Terminal
If you have some other terminal emulator that supports the same focus control sequences as iTerm 2/xterm (for the details, see
doc/vitality.txt, or search for “Focus” xterm’s ctlseqs), then it should fairly easy to extend Vitality to detect your terminal emulator (or you could just fake being iTerm 2 by setting the ITERM_PROFILE environment variable (
export ITERM_PROFILE=not-really-iTerm)—the environment variable is the only thing that Vitality uses to check for iTerm 2)