使用 vimscript 切换选项卡?

switch tab with vimscript?

我想做这样的事情:

  1. 检查是否存在名为 __Potion_Bytecode__ 的拆分
  2. 如果存在,切换到名称为
  3. 的拆分
  4. 如果没有,新建一个名为 __Potion_Bytecode__
  5. 的拆分

这是我现在正在做的代码:

function! PotionShowBytecode()
    " Here I need to check if split exists
    "  open a new split and set it up
    vsplit __Potion_Bytecode__
    normal! ggdG
endfunction

lh-vim-lib, I have lh#buffer#jump() that does exactly that. It relies on another function 中找到缓冲区可能所在的 window。

" Function: lh#buffer#find({filename}) {{{3
" If {filename} is opened in a window, jump to this window, otherwise return -1
function! lh#buffer#find(filename)
  let b = bufwinnr(a:filename) " find the window where the buffer is opened
  if b == -1 | return b | endif
  exe b.'wincmd w' " jump to the window found
  return b
endfunction

function! lh#buffer#jump(filename, cmd)
  let b = lh#buffer#find(a:filename)
  if b != -1 | return b | endif
  call lh#window#create_window_with(a:cmd . ' ' . a:filename)
  return winnr()
endfunction

它使用 another function 解决极其烦人的 E36:

" Function: lh#window#create_window_with(cmd) {{{3
" Since a few versions, vim throws a lot of E36 errors around:
" everytime we try to split from a windows where its height equals &winheight
" (the minimum height)
function! lh#window#create_window_with(cmd) abort
  try
    exe a:cmd
  catch /E36:/
    " Try again after an increase of the current window height
    resize +1
    exe a:cmd
  endtry
endfunction

如果您想使用制表符,则必须改用制表符* 函数。