vim-tmux-focus-events icon indicating copy to clipboard operation
vim-tmux-focus-events copied to clipboard

Focus Events Not Firing

Open Morley93 opened this issue 10 years ago • 29 comments
trafficstars

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

  1. Open tmux in gnome-terminal.
  2. Enter the tmux ex command :set -g focus-events on
  3. Open a file with vim.
  4. Open the file from step 3 in another editor, make modifications and save.
  5. 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

vimversion

Morley93 avatar Mar 24 '15 21:03 Morley93

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!

bruno- avatar Apr 02 '15 13:04 bruno-

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

adambiggs avatar Apr 02 '15 19:04 adambiggs

Hey, I couldn't make this work with neovim. It's "documented" in the readme section here.

Can you please try vanilla vim?

bruno- avatar Apr 03 '15 00:04 bruno-

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?

Morley93 avatar Apr 03 '15 14:04 Morley93

@bruno- oops I missed that note about it not working in NeoVim.

adambiggs avatar Apr 07 '15 17:04 adambiggs

@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

bruno- avatar Apr 08 '15 12:04 bruno-

I can confirm that on my setup, the output of both of those commands is as you described.

setcmds

Morley93 avatar Apr 09 '15 22:04 Morley93

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.

jasonbcox avatar May 18 '15 22:05 jasonbcox

Hey guys, sorry to see this is still not working for some.. Here are debugging steps to use within tmux:

  1. run $ tmux show -s, output should contain focus-events on

  2. in vim run:

    :set <F24>
    :set <F25>
    

    the output should look like this:

    t_FE <F24>       ^[[O
    t_FF <F25>       ^[[I
    
  3. 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 foo message in vim.

  4. 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 autoread in 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.

Hopefully the above steps help, let me know if you get stuck at any point.

bruno- avatar May 19 '15 11:05 bruno-

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:

  1. Open foo.txt in terminal/tmux/vim.
  2. Open foo.txt in Sublime, make a change and save.
  3. Refocus the terminal.
  4. The change in the file is detected by vim (no need to switch tmux pane/window).

Is this functionality not supported?

Morley93 avatar May 20 '15 22:05 Morley93

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..

bruno- avatar May 21 '15 11:05 bruno-

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 avatar Oct 23 '15 13:10 expipiplus1

@expipiplus1 i use neovim. Thanks you very much

SchDen avatar Nov 14 '15 22:11 SchDen

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.

bruno- avatar Nov 14 '15 23:11 bruno-

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.

ghost avatar Nov 19 '15 01:11 ghost

@Pyrohh within terminal you use?

SchDen avatar Nov 27 '15 15:11 SchDen

This work for me, but i not see this message (after focus):

SchDen avatar Nov 27 '15 15:11 SchDen

@SchDen

I'm pretty sure that message is to be expected, as long as it happens immediately after FocusGained.

ghost avatar Nov 27 '15 19:11 ghost

@Pyrohh, please open a PR for the readme update.

bruno- avatar Nov 29 '15 18:11 bruno-

@bruno-, sure thing, I just opened one.

ghost avatar Nov 29 '15 18:11 ghost

Can the readme be updated to either remove the line or just change it to say "not needed with Neovim" instead of "not working"?

justinmk avatar Mar 06 '16 09:03 justinmk

It also does not work for switching native windows in gnome-terminal but xterm handles it perfectly.

P4Cu avatar Jul 29 '16 07:07 P4Cu

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.

ericboehs avatar Sep 24 '16 20:09 ericboehs

Okay so this is weird. If I:

  1. Open vim
  2. Run :au FocusGained * echo 'foo'
  3. 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).

ericboehs avatar Sep 27 '16 18:09 ericboehs

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.

ericboehs avatar Sep 27 '16 20:09 ericboehs

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

alexpopov avatar Dec 05 '19 16:12 alexpopov

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 avatar Dec 05 '19 16:12 ericboehs

@ericboehs Thank you for the speedy response! I'll take a look :)

alexpopov avatar Dec 05 '19 17:12 alexpopov

Focus events work by default in Neovim, so I'm not sure why one would use this plugin there.

justinmk avatar Dec 06 '19 04:12 justinmk