Ⅰ. 插件描述
create files from templatesⅡ. 基本信息
|
Ⅲ. 安装方法
使用Vundle管理器安装
在你的.vimrc下添加:Plugin 'tibabit/vim-templates-are-made-of'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall
对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。
使用NeoBundle管理器安装
在你的.vimrc下添加:NeoBundle 'tibabit/vim-templates-are-made-of'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall
使用VimPlug管理器安装
在你的.vimrc下添加:Plug 'tibabit/vim-templates-are-made-of'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall
使用Pathogen管理器安装
在终端中运行以下命令:cd ~/.vim/bundle
git clone https://github.com/tibabit/vim-templates
Ⅳ. 文档说明
# vim-templates (vimawesome)
Create files from predefined templates. Templates are files with .template extension.
Installation
Vundle
Plugin 'tibabit/vim-templates'vim-plug
Plug 'tibabit/vim-templates'Usage
Using default templates
TemplateInit- Takes 0 or 1 argument, initializes file with the template and expands all placeholders defined in the template.
The argument provided can be either the name of the template file, in most cases the extension of the file, but it can be anything. e.g. you can define template for all java files in java.template and you can define template for Program.java in Program.java.template or for Makefile in Makefile.template. (e.g.:TemplateInit cpp,:TemplateInit main.cpp)
If no argument is provided filename and extension is extracted from file name (e.g. :TemplateInit).
NOTE: All file names are case sensitive.
TemplateExpand- Does not take any argument, expands all the placeholders present in file.
Helpful for updating an existing file
Auto initialization
By default auto initialization is set to true, so whenever a new file is created,
the file is automatically initialized if a template matches (for file name or file extension, since file name is more specific, it is given priority over file extension).
This can be disabled by setting g:tmpl_auto_initialize to 0 in your .vimrc.
Customization
Creating your own templates
- Create a file
<template_name>.templateinside a folder which is searched
by the plugin( see below),
e.g. if you want to create a template file for a c++ main file you
can name it cppmain.template or cppm.template
- Open the file and edit, for example
h.template
/**
* @author : {{NAME}} ({{EMAIL}})
* @created : {{TODAY}}
* @filename : {{FILE}}
*/
#ifndef {{MACRO_GUARD}}
#define {{MACRO_GUARD}}
/* declarations */
#endif /* {{MACRO_GUARD}} */generates this for a file named header.h
/**
* @author : Vikas Kesarwani (vikash@abc.com)
* @created : 07/09/2018
* @filename : header
*/
#ifndef HEADER_H
#define HEADER_H
/* declarations */
#endif /* HEADER_H */{{NAME}},{{EMAIL}},{{FILE}}and{{TODAY}}
- above are placeholders, which are expanded as soon as you call
:TemplateExpand.
- In a new file type
:TemplateInit cppmainto both place the above
content inside the file and expand the placeholders. - If templte file name (after removing
.templatefrom file name) matches the current file name or extension it is automatically imported and expnded when you create a new file. - You can also use
TemplateAutoInitvim command to import and expand templates. This command will insert and expand the template at line below the cursor.
Search paths
The plugin searches for templates as follows
- In folders named
templatesrecursively up the directory tree,
i.e. first in a directorytemplatesunder the current working
directory, then in ../templates, then '../../templates', ...
- In search paths defined by
g:tmpl_search_pathsin your.vimrcfile - The
templatesfolder in this plugin's directory
If you want to add a custom directory to the search path,
e.g. if you placed them inside a templates directory under $HOME then
add the following line in your .vimrc file:
let g:tmpl_search_paths = ['~/templates']Configuring the placeholder values
- The values into which certain placeholders expand may be influenced
by settings in your.vimrc. For examplePROJECTexpands into the
value of the variable g:tmpl_project. For more details see the
list below.
- Other than that the expansion may also be influenced on a per-directory basis.
If a matching template file is found in one of the directories of the
search path, the plugin also checks whether a file called tmpl_settings.vim
exists in the same directory. Note, than no other search directories
are checked.
If this is the case all its settings take preference over the .vimrc
- For example: In general you want the placeholder
EMAILto expand to
john.doe@example.comin your templates, hence you place
let g:tmpl_author_email = 'john.doe@example.com'in your .vimrc.
In the projects inside the folder $HOME/my_cool_stuff, however,
you want your templates to show the email address johns_projects@example.com.
So inside $HOME/templates you place a file $HOME/my_cool_stuff/tmpl_settings.vim
with content
let g:tmpl_author_email = 'johns_projects@example.com'and all template files in $HOME/my_cool_stuff will now have EMAIL
expanding to the latter value.
Placeholders
The Following placeholders are currently supported by this plugin
Date & Time
DAY: Day of the week in short form (Mon, Tue, Wed, etc,)DAY_FULL: Day of the week in full (Monday, Tueseday, etc.)DATE: Date of the month (01 to 31)MONTH: Month of the year (01 to 12)MONTH_SHORT: Short name of the month (Jan, Feb, Mar, etc.)MONTH_FULL: Full month name (January, February, etc.)YEAR: current year (2016)TODAY: Todays date in dd/mm/yyyy formatTIME: Current time in 24 our formatTIME_12: Current time in 12 hour formatTIMESTAMP: Current Timestamp, e.g.: Sunday Nov 27, 2016 15:33:33 IST
Authoring
NAME: Name of the author,g:tmpl_author_name, default :$USERHOSTNAME: Name of the host machine,g:tmpl_author_hostname, default :$HOSTNAMEEMAIL: Email of the author,g:tmpl_author_email, default :$USER@$HOSTNAME
File name
FILE: Basename of the filefilename.ext -> filenameFILEE: Filename with extensionfilename.ext -> filename.extFILEF: Absolute path of the file/path/to/directory/filename.extFILER: Filepath relative to the current directory (pwd)/relative/to/filename.ext
License and Copyright
LICENSE: License of the project,g:tmpl_license, default :MITLICENSE_FILE: Reads lincese from license file onto the next line,g:tmpl_license_file. If no file path is provided then file is read in following order-- LICENSE
- LICENSE.txt
- LICENSE.md
- license.txt
- license.md
COPYRIGHT: Copyright message,g:tmpl_copyright, default :Copyright (c) g:tmpl_company
Others
PROJECT: Name of the project,g:tmpl_project, default: not expandedCOMPANY: Name of the company,g:tmpl_company, default: not expandedMACRO_GUARD: Macro guard for use in c/c++ files.filename.h -> FILENAME_H. All dots(.) and dashes (-) present in filename are converted into underscores (_).MACRO_GUARD_FULL: Same asMACRO_GUARD, except relative path is used in place of file name. e.g.relative/to/filename.h -> RELATIVE_TO_FILENAME_HCLASS: class name, same asFILECAMEL_CLASS: class name converted to camel case (first letters of words capitalized and underscores removed)CURSOR: This is a spacial placeholder, it does not expand into anything but the cursor is placed at this location after the template expansion