0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

neovimでテンプレートファイルをサクッと適用するプラグインを作った

0
Posted at

はじめに

新しいファイルを作るたびに毎回同じような定型コードを書いていて、スニペット(vsnip)で管理しようとしたものの、複数行のテンプレートはJSONの中だと見づらいし編集もしんどい。数が増えてくると目的のスニペットを探すのも一苦労になってきたので、普通のファイルとしてテンプレートを管理して選んで貼るだけのプラグインを作りました。

できること

  • 事前に用意したテンプレートファイルを現在のバッファに適用する
  • filetype別にテンプレートを分けられる
  • Telescopeでプレビュー付きで選べる
  • :TempasteコマンドでTab補完も効く

インストール

lazy.nvimの場合:

{
  "rytkmt/tempaste.nvim",
  dependencies = {
    "nvim-telescope/telescope.nvim", -- optional
  },
  opts = {
    templates_dir = "~/.config/nvim/templates/common",
    filetype_templates_dir = "~/.config/nvim/templates/ft",
  },
}

テンプレートの配置

以下のようなディレクトリ構成でテンプレートファイルを置いておく。

~/.config/nvim/templates/
├── common/           -- 全filetypeで使える共通テンプレート
│   └── mit_license.txt
└── ft/               -- filetype別(サブディレクトリ名 = filetype名)
    ├── ruby/
    │   └── service.rb
    ├── typescript/
    │   └── component.tsx
    └── python/
        └── script.py

rubyファイルを編集中なら common/ + ft/ruby/ のテンプレートが候補に出る。

使い方

コマンドで使う

:Tempaste service.rb

Tab補完が効くので、:Tempaste + Tabで候補が出る。

Telescopeで使う

require("telescope").load_extension("tempaste")
:Telescope tempaste

プレビューでテンプレートの中身を確認してから適用できる。

おわりに

やっていることは「ファイル読んでバッファに書き込む」だけなので非常にシンプル。スニペットだと管理が煩雑になりがちなプロジェクト固有のボイラープレート(設定ファイルのひな形とか)を置いておくと便利。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?