vim函数: 快捷键在当前window下打开一个已经存在的terminal buffer(terminal buffer只能有一个)

说一下需求, 用vim几乎离不开buffer, 尤其结合send-to-terminal插件和tmux就更加离不开. 于是写了一个函数, 作用就是, 用<leader>to快捷键, 就会在当前窗口下打开新窗口, 并且载入terminal buffer, 并且focus在window的上面.

function! NewWindowOldTerminal()
" get bufnr
" let old_bufnr = bufnr("%")
" save old_bufnr to recover focus
let terminal_bufnr=filter(copy(getwininfo()), {k,v -> v.terminal == 1})[0].bufnr
wincmd n
execute "b " . terminal_bufnr
" execute old_bufnr . 'wincmd w'
" 由于不知道怎么切换focus到指定window, 放弃
wincmd k
endfunction

nnoremap <leader>to :call NewWindowOldTerminal()<CR>
posted @ 2021-05-30 22:16  Tokubara  阅读(96)  评论(0编辑  收藏  举报