用vimscript切换标签?

时间:2016-02-16 08:54:11

标签: vim vim-plugin

我想做这样的事情:

  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
    

1 个答案:

答案 0 :(得分:1)

lh-vim-lib中,我lh#buffer#jump()就是这样做的。它依赖于another function来找到缓冲区所在的窗口。

" 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

如果您想使用标签,则必须使用标签*功能。