Take the 2-minute tour ×

I have some autocmds which run a Vimscript function. The function is bound to TabEnter and also BufEnter.

However when somebody enters a new tab, the function is run several times because both the autocmds fire (and TabEnter seems to fire BufEnter with the previous "current" buffer before firing it again with the new buffer...I think).

Anyway, how can I just run my function once when somebody enters a new tab?

share|improve this question
WinEnter will be fired as well; the events are independent of each other. –  Ingo Karkat Apr 19 '13 at 8:01
According to the TabEnter help, it is triggered after WinEnter but before BufEnter, hence the behaviour you are seeing. Is there any reason you need the TabEnter mapping at all, given that BufEnter does the job? –  Prince Goulash Apr 19 '13 at 8:03
I use the TabEnter event to run the function against each buffer in the newly entered tab. After doing that I don't need to run the function again when I enter the first buffer on the new tab. –  Andy Stewart Apr 19 '13 at 8:32
add comment

2 Answers

You didn't tell us when exactly your functionality needs to be triggered; maybe your combination of events (TabEnter and BufEnter are somewhat unrelated; what if the same buffer is displayed in both tabs?) is off, and the problem could be fixed by choosing other events.

Apart from that, you could:

  • store the last processed buffer (bufnr('')) / window (winnr()) / ... in a script-local variable, and short-circuit your function if it's the same
  • if the problem is a long delay in your function, store a timestamp (localtime()) and short-circuit when too little time passed
  • if you have the tab-switching under your control (but you probably don't), :noautocmd and :set eventigore+=... could remedy this
share|improve this answer
add comment
up vote 0 down vote accepted

I solved this by having:

  • TabLocal set a tab-local variable on TabEnter and then process all the buffers in the new tab
  • BufEnter look for the tab-local variable; if present, it clears it and does nothing else; if absent, it processes the buffer.

Here's my code.

share|improve this answer
add comment

Your Answer


