How to move to non-hidden buffer that exists in some window on some tab?
I work in Vim with lots of open buffers in various windows on various tabs. I know it's simple to just load one of the existing buffers into my current window. However, some of my buffers for large files use folding expressions that cause delay of several seconds when loading them into new window. On the other hand, if they are already open in window on a tab page I just need to hop to that tab page and that window.
I gather there's no command to do this directly. Or is there?
To find window with 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 has my desired buffer loaded
- move cursor to found window OR open buffer in window on new tab if not found
Has someone already written this? Or am I missing simpler way of getting what I want?
UPDATE: The 'switchbuf' answers were exactly what I was looking for, but I still may end up using a custom function because (1) I was not understanding the behavior of 'newtab' option, since windows were still being split, and (2) my function accommodates searches for unloaded files:
function! Sbuf(filename)
let myvar = ''
tabdo let myvar = bufwinnr(a:filename) > 0 ? 开发者_JAVA技巧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
Just set the switchbuf
option:
:set switchbuf=useopen,usetab
:sbuf [filename]
If you don't want the switchbuf
option turned on all the time then you will need to write a function which changes and then resets the switchbuf
option.
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
If I'm not missing something, the behavior you talking about is controlled by switchbuf
option. It defines where to look for the buffer user demanding (current window, all windows in other tabs, or nowhere, i.e. open file from scratch every time) and how to open the buffer (in the new split, tab, or in the current window). So I think,
set switchbuf=usetab
could solve the problem for you. This orders Vim not to open the file, but to jump to the window that contains the buffer, even if that window is located in other tab page. For additional options, take a look at :h switchbuf
.
精彩评论