Andy Stewart
Andy Stewart

Reputation: 5498

In Vim, when entering a new tab, how to ignore BufEnter event?

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?

Upvotes: 0

Views: 491

Answers (2)

Andy Stewart
Andy Stewart

Reputation: 5498

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.

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172698

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

Upvotes: 1

Related Questions