Skip to content

Instantly share code, notes, and snippets.

@koturn
Last active February 10, 2018 10:45
Show Gist options
  • Save koturn/a6047753c327f489a752212099454896 to your computer and use it in GitHub Desktop.
Save koturn/a6047753c327f489a752212099454896 to your computer and use it in GitHub Desktop.
既に開いているバッファに移動するコマンド定義
if exists('*win_gotoid')
function! s:buf_open_existing(bname) abort " {{{
let bnr = bufnr(a:bname)
if bnr == -1
echoerr 'Buffer not found:' a:bname
return
endif
let wids = win_findbuf(bnr)
if empty(wids)
new
execute 'buffer' bnr
else
call win_gotoid(wids[0])
endif
endfunction " }}}
else
function! s:buf_open_existing(bname) abort " {{{
let bnr = bufnr(a:bname)
if bnr == -1
echoerr 'Buffer not found:' a:bname
return
endif
let tindice = map(filter(map(range(1, tabpagenr('$')), '{"tindex": v:val, "blist": tabpagebuflist(v:val)}'), 'index(v:val.blist, bnr) != -1'), 'v:val.tindex')
if empty(tindice)
new
execute 'buffer' bnr
else
execute 'tabnext' tindice[0]
execute bufwinnr(bnr) 'wincmd w'
endif
endfunction " }}}
endif
command! -bar -nargs=1 -complete=buffer Buffer call s:buf_open_existing(<f-args>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment