vim-tmux-focus-events
vim-tmux-focus-events copied to clipboard
Focus Events Not Firing
I recently installed this plugin and can't get it to trigger focus events as advertised. My installation was performed using the Plug plugin manager as specified on the readme. I have put set -g focus-events on into my tmux.conf file and made sure it is sourced prior to testing. I have also tried setting tmux focus events manually as an ex command as described in the reproduction steps. Additionally, I have tried using my preferred terminal emulator (urxvt) with the same result.
Details
OS: Arch Linux vim version: 7.4 (up to date from Arch Linux gvim package at the time of posting, configuration attached). tmux version: 1.9a Gnome terminal version: 3.14.2-1
Steps to reproduce
- Open tmux in gnome-terminal.
- Enter the tmux ex command
:set -g focus-events on - Open a file with vim.
- Open the file from step 3 in another editor, make modifications and save.
- Refocus vim.
Expected behaviour: Vim immediately gives a notification that the file has changed, prompting to reload it by pressing L. Actual behaviour: Vim gives no notification that the file has changed.
Thanks in advance for any support on this.
Attachments

Hi, thank you for the detailed info about the issue.
I think the issue might be in step 2. You're doing this :set -g focus-events on *after* tmux started. Did you try setting this option in your tmux.conf and *starting* tmux process with that option?
See this comment from the guy that had a similar issue in vitality.vim plugin.
Let me know the result!
I'm having the same problem in NeoVim/iTerm on mac. I have focus-events turned on in my tmux.conf.
tmux 1.9a zsh 5.0.7 NeoVim 0.0.0-alpha+201504021155 iTerm2 2.9.20150330-nightly
Hey, I couldn't make this work with neovim. It's "documented" in the readme section here.
Can you please try vanilla vim?
Just to clarify, I do have set -g focus-events on in my tmux.conf to ensure that the option is set when the tmux process starts. I have also tried this on another of my Arch Linux machines (with both gnome-terminal and urxvt) with the same results.
Perhaps another of my vim/tmux plugins/options is conflicting and preventing the plugin from working?
@bruno- oops I missed that note about it not working in NeoVim.
@Morley93, what is the result of running this in vim's command line:
:set <F24>
:set <F25>
The output should look like this for F24 and F25 respectively
t_FE <F24> ^[[O
t_FF <F25> ^[[I
I can confirm that on my setup, the output of both of those commands is as you described.

I also tried the same procedure as @Morley93 and I couldn't get the focus events to fire. I'm also using tmux 1.9a, but I'm on OSX using Terminal.app.
Hey guys, sorry to see this is still not working for some.. Here are debugging steps to use within tmux:
-
run
$ tmux show -s, output should containfocus-events on -
in vim run:
:set <F24> :set <F25>the output should look like this:
t_FE <F24> ^[[O t_FF <F25> ^[[I -
if the above 2 steps are both ok, then run this in vim's command line:
au FocusGained * echo 'foo'This will setup a simple "trigger" for focus gained event. Now move to another tmux window and focus the window with vim again. You should now see
foomessage in vim. -
If step 3 ran ok, everything is fine. But to verify file autoread is working fine, you can do these steps:
- make sure you have
set autoreadin your.vimrc - pick a random file and in tmux window 1 open
vim <file> - in tmux window 2 open another instance of vim with the same file
vim <file> - in vim in tmux window 2 make a small change to the file, for example add an empty line somewhere
- now move back to tmux window 1 where the first vim instance is running. You should see the file change automatically without any prompts from vim.
- make sure you have
Hopefully the above steps help, let me know if you get stuck at any point.
Thanks for the input @bruno-, I can confirm that all of the steps you provided are working as described when I have autoread set. I can see that the focus event fires whenever the tmux pane containing vim gains focus (when you switch to that pane either from a different pane or a different tmux window).
In my initial testing, I was expecting the event to fire when changing focus between different windows in my window manager. For instance:
- Open
foo.txtin terminal/tmux/vim. - Open
foo.txtin Sublime, make a change and save. - Refocus the terminal.
- The change in the file is detected by vim (no need to switch tmux pane/window).
Is this functionality not supported?
Hi @Morley93, thanks for confirming!
About the question "I was expecting the event to fire when changing focus between different windows in my window manager": yea, I see how that would be very useful. I think this might be dependent on the terminal application you're using. I just tested this on OSX and here's some quick results:
- it's *working* for iTerm when tmux is running inside the window
- not working for iTerm running plain bash + vim inside (no tmux)
- not working for
Terminal.app(with or without tmux)
From what I know iTerm has a very good integration with Tmux so I think that's why the first case is woking.
So, if I'm not wrong, it's up to terminal applications to implement "focus gained", "focus lost" functionality. Please correct me if someone has more knowledge about this..
I've opened a pull request to add support for focus events in neovim (neovim/neovim#3488). I'd be grateful to anyone willing to give this a test :) It's working very well for me at the moment.
@expipiplus1 i use neovim. Thanks you very much
Hi @expipiplus1, thanks, I've added a note about your PR in the readme so people can upvote the feature. Hope it's merged soon.
Hi @bruno-, https://github.com/neovim/neovim/pull/3488 was just merged and your plugin now works for me, so perhaps the README should be updated. I didn't submit a PR to update the README as I'm not sure if focus events are working for others as well.
@Pyrohh within terminal you use?
This work for me, but i not see this message (after focus):

@SchDen
I'm pretty sure that message is to be expected, as long as it happens immediately after FocusGained.
@Pyrohh, please open a PR for the readme update.
@bruno-, sure thing, I just opened one.
Can the readme be updated to either remove the line or just change it to say "not needed with Neovim" instead of "not working"?
It also does not work for switching native windows in gnome-terminal but xterm handles it perfectly.
I'm getting stuck at Step 3, au FocusGained * echo 'foo'.
It doesn't output anything. <F24> is set and tmux show -s shows focus-events on.
I'm on Sierra with Terminal 2.7.1 (380). I've also tried iTerm 2 (3.0.10). I'm using tmux 2.2. Vim 8.0 2016 Sep 12; patches 1-3 included. I also had tried this on a recent version of Vim 7.4 and saw the same problem.
Okay so this is weird. If I:
- Open
vim - Run
:au FocusGained * echo 'foo' - Switch to a different tmux pane and then back again
There's no output of 'foo'.
But... if before step 3 I run :!, I do get the output of 'foo' and then everything starts working as expected. It seems calling any external command will fix it (e.g. running K to look up a word in the man page).
Figured it out!
I started commenting out everything in my vimrc and it started working.
Turns out I was calling silent !stty -ixon > /dev/null 2>/dev/null and for some reason that broke it. I was calling it so that I could use Ctrl-S and Ctrl-Q as keybindings, but that no longer seems necessary (in zsh 5.2/ mac OS Sierra).
Edit: stty -ixon is still necessary. Moving it to my .zshrc instead of .vimrc is the fix.
Sorry to necro.
re: Step 2 , I do not have <F25> and <F24> set in neovim.
I'm using vim-plug to install the plugin. I have tried uninstalling and reinstalling. Is the plugin supposed to set those bindings?
Setup: Tmux 2.8 Nvim 0.5.0 CentOS 7
Yes. It looks like vim-tmux-focus-events sets bindings for F24/F25.
I don't know how to help you specifically but I can point you to my dotfiles which work out of the box on a new Mac (hopefully it'll help you with CentOS).
The commit where I added this plugin is f463e37b4. I think that includes everything needed; and most of it is just tweaks for enable/disbling my relative number stuff which you can ignore. (I'm also using vim-diminactive which dims vim on focus lost.)
Tmux 2.9a Nvim 0.4.3 macOS 10.15.1
@ericboehs Thank you for the speedy response! I'll take a look :)
Focus events work by default in Neovim, so I'm not sure why one would use this plugin there.