📟
Neovim用のターミナルプラグインを作った話
はじめに
これは Vim駅伝 4/24の記事です。
自作プラグインの宣伝記事になります。
これはなに
Neovim 専用のターミナルプラグインです。
いくつかのコマンドを提供します。
- 操作デモ
 
インストール・設定
Lua 製のプラグインですが、インストールするだけで使えます。
setup() は設定を変更したい場合に使ってください。
require("ugaterm").setup({
  ---@type string The terminal buffer name prefix.
  prefix = "terminal://",
  ---@type string The filetype for a terminal buffer.
  filetype = "ugaterm",
  ---@type string|function The command/function to open a teminal window.
  open_cmd = "botright 15sp",
  -- Example of opening in a floating window.
  --
  -- open_cmd = function()
  --   local height = vim.api.nvim_get_option("lines")
  --   local width = vim.api.nvim_get_option("columns")
  --   vim.api.nvim_open_win(0, true, {
  --     relative = "editor",
  --     row = math.floor(height * 0.1),
  --     col = math.floor(width * 0.1),
  --     height = math.floor(height * 0.8),
  --     width = math.floor(width * 0.8),
  --   })
  -- end,
})
- prefix
- これに番号を付けたものが terminal buffer 名になります(
terminal://1,terminal://2, etc.)。 
 - これに番号を付けたものが terminal buffer 名になります(
 - filetype
- terminal buffer に設定されるファイルタイプです。
 
 - open_cmd
- terminal window を開くためのコマンドです。文字列 or Lua 関数を受け付けます。
 - 文字列は EX コマンドとして解釈されます(
vim.cmd()に渡されます)。 - デフォルトでは分割です(下、15行)。IDE ぽいですね。
 
 
使い方
EX コマンドを使って terminal を操作します。
:UgatermToggle, :UgatermNew, :UgatermSelect あたりがよく使うと思うので、マッピングするならこれらかなと思います。
増える可能性もあるので、使う際には一応 doc も見てください。
- 
:UgatermOpen- terminal window を開きます。
 - 最近使用した(Most Recently Used) terminal buffer が選ばれます。
 - terminal buffer が一つもない場合は 
:UgatermNewが呼び出されます。 
 - 
:UgatermNew- 新規で terminal buffer を開きます。
 - terminal window は、開いていなければ開きます。
 
 - 
:UgatermHide- terminal window を閉じます。
 
 - 
:UgatermToggle- 
:UgatermOpenと:UgatermHideをトグルします。 
 - 
 - 
:UgatermSelect- 
vim.ui.select()を使用して terminal buffer を選択します。 
 - 
 - 
:UgatermDelete- 現在開いている terminal buffer を削除します。
 - 他に terminal buffer がある場合は terminal windows は維持され、次の buffer を開きます。
 - その buffer が最後の場合は、window も閉じます。
 
 - 
:UgatermRename [{newname}]- 現在の terminal buffer の名前を変更します。
 - 
{newname}を省略した場合はvim.ui.input()が使用されます。 
 
最後に
とても多機能だったり特別な機能があるわけではありませんが、実装をシンプルにしておりメンテナンス性は高いです。
近年の Neovim plugin 界隈は流行り廃りが非常に激しいため、自作して自分でメンテすることにしました。
良ければ使ってみてください。
Discussion