如何移动到某个选项卡上某个窗口中存在的非隐藏缓冲区?

发布于 2024-09-27 08:50:22 字数 881 浏览 4 评论 0原文

我在 Vim 中工作,在不同窗口的不同选项卡上有很多打开的缓冲区。我知道将现有缓冲区之一加载到当前窗口中很简单。但是,我的一些大文件缓冲区使用折叠表达式,这会导致将它们加载到新窗口时延迟几秒钟。另一方面,如果它们已经在选项卡页上的窗口中打开,我只需要跳转到该选项卡页和该窗口。

我认为没有命令可以直接执行此操作。或者有吗?

要查找具有所需缓冲区的窗口,我可以编写一个函数来执行此操作:

  1. 对于每个
  2. 选项卡上每个窗口的每个选项
  3. 卡,检查每个窗口以查看它是否已加载所需的缓冲区,
  4. 将光标移动到找到的窗口,或者在新选项卡上的窗口中打开缓冲区,如果没有找到

有人已经写过这个了吗?或者我错过了获得我想要的东西的更简单的方法?

更新:“switchbuf”的答案正是我正在寻找的,但我仍然可能最终使用自定义函数,因为(1)我不理解“newtab”选项的行为,因为窗口仍然被分割,并且( 2)我的函数可以搜索已卸载的文件:

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

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:

  1. for each tab
  2. for each window on each tab
  3. check each window to see if it has my desired buffer loaded
  4. 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 ? 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

背叛残局 2024-10-04 08:50:22

只需设置 switchbuf 选项:

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

如果您不希望 switchbuf 选项始终打开,那么您需要编写一个函数来更改然后重置 >switchbuf 选项。

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

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
夏日落 2024-10-04 08:50:22

如果我没有遗漏什么,您所说的行为是由 switchbuf 选项控制的。它定义了在哪里寻找用户需要的缓冲区(当前窗口、其他选项卡中的所有窗口或无处,即每次从头开始打开文件)以及如何打开缓冲区(在新的拆分、选项卡中或在当前窗口中) )。所以我想,

set switchbuf=usetab

可以为你解决这个问题。这命令 Vim 不打开文件,而是跳转到包含缓冲区的窗口,即使该窗口位于其他选项卡页中。有关其他选项,请查看 :h switchbuf

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文