switch tab with vimscript?

我怕爱的太早我们不能终老 提交于 2020-01-07 03:07:24

问题


I want do something like this:

  1. check if a split named __Potion_Bytecode__ exists
  2. if exists, switch to the split with name
  3. if not, new a split named __Potion_Bytecode__

Here is code I am now doing:

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:


In lh-vim-lib, I have lh#buffer#jump() that does exactly that. It relies on another function to find a window where the buffer could be.

" 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

Which uses another function to work around the extremely annoying 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

If you want to work with tabs, you'll have to use tab* functions instead.



来源:https://stackoverflow.com/questions/35427862/switch-tab-with-vimscript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!