I use the following setting for automatic line number toggle.

augroup numbertoggle
  autocmd BufEnter,FocusGained,InsertLeave * set relativenumber
  autocmd BufLeave,FocusLost,InsertEnter   * set norelativenumber
augroup END

But it seems that this setting does not work inside tmux. If I open two tmux panes side by side and open a file with Neovim in one pane and then switch to another tmux pane, the relative line number in Neovim does not change to absolute number. How to fix this issue?

Besides, I use set -g mouse on inside ~/.tmux.conf so that I can use mouse clicks to change tmux panes, if that information is relevant.

  • Im not sure about the Focus events, but you havent left the buffer or entered insert mode when you switch, so it makes sense those dont trigger.
    – D. Ben Knoble
    Commented Jan 10, 2019 at 14:12
  • Could you rename this question to something like "FocusGaine/Lost not detected in tmux"? And perhaps combine your own answer and the answer of @BLayer (and than accept it)? That way we would get a proper answered question, that covers NeoVim and Vim.
    – Ralf
    Commented Jan 12, 2019 at 23:45
  • The plugin was written for both neovim and vim users. I guess it's possible that there's since been a general fix applied to neovim but the neovim dev's words don't indicate one way or the other. They, like OP, could just happen to have a terminal type that needs only the tmux config.
    – B Layer
    Commented Jan 13, 2019 at 7:14
  • @Ralf, only experienced may know what FocusGained/Lost event mean. Before I ask this question, I have never heard of these events. I don't think the new title is useful for new users. For experienced user, they may have solved this issue themselves and do not need answer here.
    – jdhao
    Commented Jan 13, 2019 at 8:14
  • My $0.02 on the subject change: Maybe there aren't many people who know the event names but there are plenty enough people who understand the notion of focus, generally speaking. You could use something like "Focus change inside tmux doesn't trigger applicable autocmd events".
    – B Layer
    Commented Jan 13, 2019 at 21:05

4 Answers 4


I opened an issue on Neovim repo and get the right answer.

We need to turn on the focus-events for tmux. Edit the tmux config file ~/.tmux.conf and add the following setting:

set -g focus-events on

Refresh the tmux session and the automatic relative line number toggle should work as expected now.

Edit: For some users using some terminal, setting focus-events for tmux solely does not work. You may also need to install the vim plugin described in B Layer's answer

  • Great, that this is solved for you. I tried it with Vim 8.1.712 and tmux 2.1 in GNOME-terminal and I couldn't get it working.
    – Ralf
    Commented Jan 11, 2019 at 12:33
  • Maybe your tmux version is too low. My tmux version is 2.8-rc.
    – jdhao
    Commented Jan 11, 2019 at 12:49
  • @Ralf See if the plugin mentioned in my answer works for you. (It has helped me.)
    – B Layer
    Commented Jan 12, 2019 at 6:25
  • In fact, I just noticed that the plugin's README mentions GNOME specifically as one of the tested terminal types so that's a hopeful sign.
    – B Layer
    Commented Jan 12, 2019 at 7:05
  • @BLayer Yes, I can confirm that with the plugin vim-tmux-focus-events, at least "FocusGained" is detected. It didn't work for "FocusLost". (GNOME-terminal, Vim 8.2.722).
    – Ralf
    Commented Jan 12, 2019 at 9:08

My understanding of this issue is that xterm-compatible terminals (at least...there may be others) emit a certain control sequence when focus is gained/lost from a terminal window. Rather than Vim, say, intercepting said sequences and triggering FocusGained/FocusLost events in response it ignores them. Fortunately someone wrote a vim/neovim plugin that will do exactly that called vim-tmux-focus-events.

It seems not all terminals need the plugin to solve the problem. As OP notes in their answer it was enough in their case to simply enable the tmux focus-events setting in their tmux.conf file:

 set -g focus-events on

And actually this is required regardless; the plugin's README instructs one to enable same.

So try the tmux setting alone first. If no-joy try the plugin, too. (I'm trying to confirm it but I believe one must be using tmux 1.8 or later for all this.)

(As a bonus the plugin also helps with getting autoread to work in terminals/tmux, too.)

Update: Just noticed that someone submitted back in '13 a patch for Vim that would obviate the need for the plugin. Doesn't look to be getting much love.


As D. Ben Knoble said in his comment, "you haven't left the buffer or entered insert mode when you switch, so it makes sense those don't trigger.

And the help for FocusGained / Lost says: "Only for the GUI version and a few console versions where this can be detected." I guess the console never lost/gained focus as you switch tmux panes. So even if the console you use could detect focus lost/gained, it would not trigger.

Looking at the source of Vim, the comment on the function ui_focus_change in ui.c says:

 * Called when focus changed.  Used for the GUI or for systems where this can
 * be done in the console (Win32).
  • I thought that clicking another tmux pane has moved the cursor out of the vim window.
    – jdhao
    Commented Jan 11, 2019 at 6:47
  • @jdhao FocusGained/Lost in the console seems Windows only. See update.
    – Ralf
    Commented Jan 11, 2019 at 7:10
  • Thanks for the update. I will add an issue to Neovim repo to see if there is a workaround.
    – jdhao
    Commented Jan 11, 2019 at 7:13
  • 1
    This depends on the terminal. I believe some terminals are actually being able to process Focus events and do. Commented Jan 11, 2019 at 11:58
  • The developer of nvim solved this issue. See my answer.
    – jdhao
    Commented Jan 11, 2019 at 12:21

Realize this is really old, but for me, substituting WinEnter and WinLeave for FocusGained and FocusLost fixed the problem in my VIM terminal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.