😊

vimでterminalをtoggleで表示、非表示する設定

に公開

経緯

neovimではターミナルをトグルするhttps://github.com/akinsho/toggleterm.nvim という大変便利なプラグインを使っていました。
しかし、このプラグインはvimでは使えないので今回ちゃちゃっとvimscriptを書きました。

実装

以下を.vimrcに書けば指定したキーマップでターミナルモードを切り変えることができます。

let g:terminal_bufnr = -1
function! ToggleTerminal()
  if &buftype == 'terminal'
    " If the current buffer is a terminal, go back to the previous buffer
    execute "buffer #"
    execute "close"
  else
    " If the current buffer is not a terminal, try to find the terminal buffer
    if bufexists(g:terminal_bufnr)
      " If the terminal buffer exists, switch to it
      execute 'split'
      execute "buffer " . g:terminal_bufnr
      execute "normal i"

    else
      " If no terminal buffer exists, create a new one and save its buffer number
      terminal
      let g:terminal_bufnr = bufnr('%')
    endif

  endif
endfunction

nnoremap <silent> <C-t> :call ToggleTerminal()<CR>
tnoremap <silent> <C-t> <C-\><C-n>:call ToggleTerminal()<CR>

このままコピペすれば、normal modeにてCtrl + tでターミナルをトグルすることができます。

GitHubで編集を提案

Discussion