[英]VIM: Close all other tabs of current file except the one I'm on?
Say if my 5 open tabs are:假设我的 5 个打开的标签是:
file1 file2 file2 file2 file3
I want a quick way to close just all of the duplicate file2
tabs, when the focus is on any of the file2
tabs.当焦点位于任何
file2
选项卡上时,我想要一种快速关闭所有重复的file2
选项卡的方法。
So result after running command should be:所以运行命令后的结果应该是:
file1 file2 file3
The command should not have any effect when focus is on file1
or file3
as there're no duplicate tabs of these.该命令不应该有任何效果时,重点是在
file1
或file3
,因为是这些没有重复的标签。
Any thoughts on how to achieve this?关于如何实现这一目标的任何想法?
EDIT - SOLUTION based on Ingo's suggestions:编辑 - 基于 Ingo 建议的解决方案:
function! s:CloseDupTabs()
let curBufnr = bufnr('')
let numTabs = tabpagenr('$')
" Run for 1 lesser than numTabs so that we don't visit current tab in loop
for i in range(numTabs - 1)
execute 'tabnext'
if curBufnr == bufnr('')
" Check edge case to see if we're already on last tab before doing tabprev
if tabpagenr() != tabpagenr('$')
close
execute 'tabprev'
else
close
endif
endif
endfor
execute 'tabnext'
endfunction
Every open file in Vim gets a unique buffer number ; Vim 中每个打开的文件都有一个唯一的缓冲区号; the one for your current file (
file2
in your example) can be obtained via您当前文件的一个(在您的示例中为
file2
)可以通过
:let bufnr = bufnr('')
You can then inspect the other tab pages, and close any buffer with the same number there.然后,您可以检查其他标签页,并关闭其中具有相同编号的任何缓冲区。
:help tabpagebuflist()
has an example how to iterate over all tab pages; :help tabpagebuflist()
有一个如何遍历所有标签页的例子; just modify that to skip the current ( tabpagenr()
) one.只需修改它以跳过当前(
tabpagenr()
)一个。
In order to close a buffer, you first need to navigate to the corresponding tab page:为了关闭缓冲区,您首先需要导航到相应的标签页:
:execute 'tabnext' tabnr
If you always have just one window in a tab, you can then just :close
it.如果您总是在一个选项卡中只有一个窗口,那么您只需
:close
。 Else, you need to first find the window that has the buffer, and go to it:否则,您需要先找到具有缓冲区的窗口,然后转到它:
:execute bufwinnr(bufnr) . 'wincmd w'
It would be nice to keep track of the original tab page number, decrementing it when removing tab pages before it, in order to return to the original one.跟踪原始标签页码会很好,在删除它之前的标签页时将其递减,以便返回到原始标签页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.