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快捷鍵效果可上面的是一樣的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章