vim配置文件模版

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.nvimgithub地址coc.nvim安装

安装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快捷键效果可上面的是一样的

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。