vim函数实现

function AddTitle()
call setline(1,"<?php")
call append(1,"")
call append(2,"/*")
call append(3," * Created by vim")
call append(4," * User: ache")
call append(5," * Date:" . strftime("%Y-%m-%d"))
call append(6," * Time:" . strftime("%H-%i"))
call append(7," * Describe:")
call append(8," */")
call append(9,"")
endf

# 新建文件时自动写入
autocmd BufNewFile *.php exec ":call AddTitle()"

# 新建文件后快捷键写入
nmap <leader>at :call AddTitle()<CR>

新建​​test.php​​即可看到以下内容

<?php

/**
* Created by Vim.
* User: ache
* Date: 2020/5/21
* Time: 11:26
* Describe:
*/

或者使用第二种方式生成

vim插件实现

安装​​coc-template​

// 安装coc-templete命令
npm i coc-template

// vim下安装coc-templete插件
:CocInstall coc-template

查看插件所在的项目目录我本地是​​~/.config/coc/extensions/node_modules/coc-template​​。以下目录结构

.
├── README.md
├── lib
│ └── index.js
├── package.json
└── templates
├── =template=.bash
├── =template=.c
├── =template=.cmake
├── =template=.coffee
├── =template=.css
├── =template=.dart
├── =template=.f
├── =template=.f90
├── =template=.go
├── =template=.h
├── =template=.hs
├── =template=.html
├── =template=.humans.txt
├── =template=.java
├── =template=.jl
├── =template=.js
├── =template=.jsp
├── =template=.jsx
├── =template=.lhs
├── =template=.lua
├── =template=.ml
├── =template=.php
├── =template=.pl
├── =template=.pls
├── =template=.pm
├── =template=.pro
├── =template=.py
├── =template=.rb
├── =template=.robots.txt
├── =template=.rs
├── =template=.sh
├── =template=.sol
├── =template=.sql
├── =template=.tex
├── =template=.txt
├── =template=.xml
├── =template=.xsl
├── =template=.zcml
└── =template=Makefile

​templates​​​文件夹下是模版文件可以根据需要自行修改添加。​​lib​​​是脚本文件。可以在里面查看预定义的变量。修改​​=template=.php​​文件

<?php

/**
* Created by Vim.
* User: %USER%
* Date: %YEAR%/%MONTH%/%DAY%
* Time: %TIME%
* Describe:
*/

%HERE%

​%USER%​​​等变量可在​​/lib/index.js​​里面查看定义

nmap <leader>t :CocCommand template.templateTop<CR>

新建​​test.php​​​文件 使用​​<leader>t​​快捷键效果可上面的是一样的