Recent Vim versions have win_getid()
function and win_id2tabwin()
in place of the below s:FindWinID
. Also win_gotoid()
to just go to window with given identifier. Identifiers are maintained by Vim itself, so even opening window with e.g. noautocmd wincmd s
will not be able to create a window without an identifier.
For older versions, you can use window variables to get such identifier:
" put unique window identifier into w:id variable
autocmd VimEnter,WinEnter * if !exists('w:id') | let w:id={expr_that_will_return_an_unique_identifier} | endif
This should mark all windows. Or, it is maybe better to mark only that windows which you want to use just after window creation. To find a window with id abc
and then switch to it:
function s:FindWinID(id)
for tabnr in range(1, tabpagenr('$'))
for winnr in range(1, tabpagewinnr(tabnr, '$'))
if gettabwinvar(tabnr, winnr, 'id') is a:id
return [tabnr, winnr]
endif
endfor
endfor
return [0, 0]
endfunction
<...>
let [tabnr, winnr]=s:FindWinID('abc')
execute "tabnext" tabnr
execute winnr."wincmd w"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…