How to move to a non-hidden buffer that exists in a window on a tab?

I work in Vim with many open buffers in different windows on different tabs. I know that it is just easy to load one of the existing buffers into the current window. However, some of my large file buffers use folding expressions that cause a delay of several seconds when loading them into a new window. On the other hand, if they are already open in a window on a bookmark, I just need to go to this bookmark and this window.

I do not see any team for this. Or is there?

To find the window with the desired buffer, I could write a function that does this:

  • for each tab
  • for each window on each tab
  • check each window to see if it is loaded into the correct buffer.
  • move the cursor to the found window OR open the buffer in the window in a new tab, if not found

Has anyone already written this? Or did I miss an easier way to get what I want?

UPDATE: the โ€œswitchbufโ€ answers were exactly what I was looking for, but I can still use the user-defined function because (1) I did not understand the behavior of the โ€œnewtabโ€ option, since the windows were still split, and (2) my function supports search for uploaded files:

function! Sbuf(filename) let myvar = '' tabdo let myvar = bufwinnr(a:filename) > 0 ? tabpagenr() \ . ' ' . bufwinnr(a:filename) : myvar if myvar > '' silent execute split(myvar)[0] . "tabn" silent execute split(myvar)[1] . "wincmd w" else execute 'tab drop ' . a:filename endif endfunction 
+4
source share
2 answers

Just set the switchbuf parameter:

 :set switchbuf=useopen,usetab :sbuf [filename] 

If you do not want the switchbuf parameter to switchbuf on all the time, you will need to write a function that will change, and then reset the switchbuf parameter.

 function MySwitchBuf(filename) " remember current value of switchbuf let l:old_switchbuf = &switchbuf try " change switchbuf so other windows and tabs are used set switchbuf=useopen,usetab execute 'sbuf' a:filename finally " restore old value of switchbuf let &switchbuf = l:old_switchbuf endtry endfunction 
+4
source

If I am missing something, the behavior you are talking about is controlled by the switchbuf option. It determines where to look for the buffer user (current window, all windows on other tabs or nowhere, i.e. open the file every time from scratch) and how to open the buffer (in a new split tab or in the current window). Therefore I think

 set switchbuf=usetab 

may solve the problem for you. This allows Vim not to open the file, but to go to the window containing the buffer, even if this window is on a different tab. For more options, see :h switchbuf .

+3
source

All Articles