You can probably write a bunch of Vim script to achieve the desired behaviour, but I'll suggest you take another route.
In Vim, what we know as tabs in other applications, are called buffers. They are a bit confusing at first because by default there's no buffer bar, so there's no way to quickly see all the open buffers. You can install a plugin that makes the tab line or status line to show you the loaded buffers, like tabline and wintabs, but there are better ways to navigate buffers. Using a fuzzy finder like FZF is nice because it only takes screen space while you are using it. And of course, you can use the native commands :ls
(to list the buffers), :bnext
(to jump to the next one), :bprev
(to jump to the previous one). There are many more commands, see :help buffers
for details.
Now you may be wondering if Vim buffers are like other application's tabs, what is a Vim tab? They are actually called "tab pages" (see :h tabpage
) and their purpose is to hold one or more windows. That means you can have two tab pages, in one you can have two windows, side by side, in the other you could have three windows, one to the left, and the other two in the right halve, one below the other. The idea is that you can have different layouts and switch between them as desired. Plugins like DiffOrig and Mergetool are quite nice because instead of disrupting your windows layout, they create a new tab and close it once you are done.
Vim default tab line shows the buffer loaded in the currently focused window, and that makes tab pages to look like tabs in other apps. I think that's one of the reasons they are so confusing at the beginning.
If you are still not convinced I recommend you to read Buffers, windows, and tabs, and/or Vim tab madness. Both do a better job than me
set switchbuf=useopen,usetab,newtab
often works.