def yasuharu519(self):

日々の妄想

Vim でファイルの新規作成時にテンプレートファイルを読み込む

最近 javascript の勉強してて react, webpack などを触るようにしているけど、web pack.config.js を作成するときに、ボイラープレートが多すぎて入力がだるすぎた。

最初にファイルの作成するときに、テンプレートファイルのロードできないか調べてみたら vim での方法があったので設定した。

augroup load_templates
    autocmd!
    let s:load_templates_dir='~/dotfiles/templates'
    let s:load_templates_command="0read ".s:load_templates_dir
    autocmd BufNewFile *.html            execute s:load_templates_command."/template.html"
    autocmd BufNewFile webpack.config.js execute s:load_templates_command."/template.webpack.config.js"
    autocmd BufNewFile Dockerfile        execute s:load_templates_command."/template.Dockerfile"
augroup END

新しくプロジェクト始めるときに、webpack.config.js とか作成した時には、上記の例だと template.webpack.config.js を読み込んだ状態で始まる。ついでに Dockerfile もどれ設定すればいいのか結構忘れるので、テンプレートファイルを読み込んでスタートできるように設定した。

指定したパターンのファイルを開いた時に、0read コマンドでテンプレートファイルが読み込まれる様になってる。read コマンドだけだと、読み込んだデータをカーソルのところに貼り付け。0read だとファイルを読み込んだ後、一番上の行から貼り付けを行う。

template.webpack.config.js のほうはこんな感じにしておいて、この状態から編集開始することで、設定項目とかいろいろ思い出す必要なくなってよい。

   module.exports = {
      entry: './main.js',
      output: {
        path: './',
        filename: 'index.js'
      },
      devServer: {
        inline: true,
        port: 3333
      },
      module: {
        loaders: [
          {
            test: /\.js$/,
            exclude: /node_modules/,
            loader: 'babel',
            query: {
              presets: ['es2015', 'react']
            }
          }
        ]
      }
    }