vim自动加载模板的插件~
来源:互联网 发布:水槽龙头 知乎 编辑:程序博客网 时间:2024/06/06 09:27
又搜了下,貌似有个CVIM的插件也很N~~~学习了~~
记得之前实习的时候新建一个文件.h .c .cpp后会自动加一些文件说明,比如文件的名字,创建日期、创建者等等一些信息,想起来使用很方便,就自己找了下怎么做,没想到这么难搜,不过还是被我找到了~~~
CU上一个帖子:http://bbs.chinaunix.net/viewthread.php?tid=895258
1、将文件放入 plugin(~/.vim/plugin/ 或者 $HOME/vimfiles/plugin/)目录
2、.vimrc 加入:
let g:template_load = 1let g:template_tags_replacing = 1let g:T_AUTHOR = "Jestery"let g:T_AUTHOR_EMAIL = "jestery@gmail.com"let g:T_AUTHOR_WEBSITE = "http://www.pte.cn"let g:T_DATE_FORMAT = "%c"
3、新建一个模板文件存为 templates/tpl.c(例如~/.vim/templates/tpl.c)
模板名的取名规则是 tpl.extension,新建*.h的时候,tpl.h 被读入
/* *Author: * <T_AUTHOR> <<T_AUTHOR_EMAIL>> * <T_AUTHOR_WEBSITE> * * File: <T_FILENAME> * Create Date: <T_CREATE_DATE> */void main(int argc, char* argv[]){ <T_CURSOR>}/* vim: set ts=4 sw=4: */
这样就可以实现新建一个.h .c .cpp等文件时自动加载模板里面的内容了。上面是作者的做法,我的做法如下:
前两步是一样的,第三步其实也很类似,只是我把模板放在了指定的其他位置,模板的名称以template.h template.c template.cpp命名,这样每次新建同类型的文件时就会加载这些模板里面的内容。同时在如果要支持其他类型的文件,直接增加模板文件就ok了。比如要支持后缀为.*的文件,则可以增加一个template.*模板,那么新建文件时候就会加载这个模板了。很方便吧~~~哈哈~~~
没找到csdn上附件的插入位置。。
贴下代码吧,不是很长,而且很清晰
- "=============================================================================
- " Vim global plugin for load template files
- " File: template_loader.vim
- " Maintainer: Jestery Liu <jestery@gmail.com>
- " Last Change: 2007/02/02
- " Version: 0.1
- " Description:
- " Load template file for editing new files
- " Options:
- " (int) g:template_load
- " Set to 0 to disable template auto-loading, 1 for enable.
- "
- " (int) g:template_confirm
- " If set to 1, everytime you edit a new file you will be asked for
- " whether load template or not.
- "
- " (string) g:template_path
- " If set, script will look for template files in this path, if not set,
- " the script will locate template files in each path in &runtimepath.
- "
- " (string) g:template_dir_name
- " By default, if g:template_path is not set, script will locate template
- " files in &runtimepath.'/templates/', '~/.vim/templates/' for example,
- " you can custom your own directory name by set this value. If
- " g:template_path is set, this value never be used.
- "
- " (string) g:template_prefix
- " The default template file name is 'tpl.extension', for example, if you
- " create a 'foo.c', '~/.vim/templates/tpl.c' will be loaded. You can set
- " this value to 'skel' to load 'skel.c'
- "
- " (int) g:template_tags_replacing
- " If set to 1, some pre-defined tags will be replaced by the
- " following variables:
- " (string) g:T_AUTHOR
- " (string) g:T_AUTHOR_EMAIL
- " (string) g:T_AUTHOR_WEBSITE
- " (string) g:T_LICENSE
- " (string) g:T_DATE_FORMAT (Same as strftime)
- " (int) g:T_FILENAME_USE_FULL_PATH
- " (If 1, /foo/bar.c will be used, otherwise, bar.c)
- " Pre-defined tags:
- " <T_AUTHOR>, <T_AUTHOR_EMAIL>, <T_AUTHOR_WEBSITE>
- " <T_LICENSE>, <T_FILENAME>, <T_CREATE_DATE>
- " <T_CURSOR>
- " If found <T_CURSOR>, the cursor will be placed here and <T_CURSOR>
- " will be deleted.
- "
- " (int) g:template_replace_start_line
- " Start tag replacing from here. Default is 1
- "
- " (int) g:template_replace_end_line
- " End tag replacing to here. Default is the last line ("$")
- if exists("g:template_load") && g:template_load==1
- augroup Template_Loader
- autocmd!
- au BufNewFile * call LoadTemplate()
- augroup END
- else
- finish
- endif
- function! LoadTemplate()
- let s:template_file = ""
- let g:template_prefix = exists("g:template_prefix") ? g:template_prefix : 'tpl'
- let s:ext = expand('%:e')
- if s:ext == ""
- let s:ext = expand('%:t')
- endif
- let s:template_file_name = g:template_prefix . '.' . s:ext
- if exists("g:template_path")
- let s:template_file = g:template_path.'/'.s:template_file_name
- else
- let s:template_dir_name = exists("g:template_dir_name") ? g:template_dir_name : 'templates'
- let s:rtp = &runtimepath
- let s:dirs = split(s:rtp, ',')
- if empty(s:dirs)
- return
- endif
- for dir in s:dirs
- let s:template_file = dir.'/'.s:template_dir_name.'/'.s:template_file_name
- if filereadable(s:template_file)
- break
- endif
- endfor
- "unlet s:rtp, s:dirs, s:template_dir_name
- endif
- if s:template_file=="" || !filereadable(s:template_file)
- echo "Missing template file. (".s:template_file.")"
- return
- endif
- call LoadTemplateFile(s:template_file)
- endfunction
- function! LoadTemplateFile(filename)
- let choice = 1
- if exists("g:template_confirm") && g:template_confirm==1
- let choice = confirm("Do you want to load template for this new file?", "&Yes/n&No")
- endif
- if choice==0
- return
- endif
- sil! execute "0r " . a:filename
- call TemplateReplTags()
- endfunction
- function! TemplateReplTags()
- if g:template_tags_replacing != 1
- return
- endif
- let sl = exists("g:template_replace_start_line") ? g:template_replace_start_line : 1
- let el = exists("g:template_replace_end_line") ? g:template_replace_end_line : "$"
- if exists("g:T_AUTHOR")
- sil! execute sl.','.el."s/<T_AUTHOR>/".g:T_AUTHOR."/g"
- endif
- if exists("g:T_AUTHOR_EMAIL")
- sil! execute sl.','.el."s/<T_AUTHOR_EMAIL>/".g:T_AUTHOR_EMAIL."/g"
- endif
- if exists("g:T_AUTHOR_WEBSITE")
- sil! execute sl.','.el."s=<T_AUTHOR_WEBSITE>=".g:T_AUTHOR_WEBSITE."=g"
- endif
- if exists("g:T_LICENSE")
- sil! execute sl.','.el."s/<T_LICENSE>/".g:T_LICENSE."/g"
- endif
- if exists("g:T_DATE_FORMAT")
- sil! execute sl.','.el."s/<T_CREATE_DATE>/".strftime(g:T_DATE_FORMAT)."/g"
- endif
- if exists("g:T_FILENAME_USE_FULL_PATH") && g:T_FILENAME_USE_FULL_PATH==1
- let s:fn = expand("%:p")
- else
- let s:fn = expand("%:t")
- endif
- sil! execute sl.','.el."s/<T_FILENAME>/".s:fn."/g"
- unlet s:fn
- let s:curpos = search("<T_CURSOR>", 'W')
- if !empty(s:curpos)
- call cursor(s:curpos)
- sil! execute "normal df>"
- sil! execute "startinsert"
- endif
- endfunction
- " vim: set ts=4 sw=4 tw=78:
- vim自动加载模板的插件~
- vim自动加载模板的插件~
- vim模板插件vim-template的使用
- Vim Skills之新建文件时自动加载模板
- cscope插件, 项目任意子目录下开vim自动加载.
- vim插件配置模板
- PHP语法自动检查的Vim插件
- PHP语法自动检查的Vim插件
- vim自动补齐插件的使用
- Vim自动文档插件----------DoxygenToolkit
- vim自动配置(插件spf13-vim)
- vim自动加载cscope.out
- snipMate vim的自动补全代码段插件
- Ubuntu安装Vim的Python自动补全插件
- vim 的python代码自动补全插件pydiction
- vim自动补全插件YouCompleteMe的安装及配置
- 通过插件实现VIM编辑的自动补齐功能
- cscope插件,项目根目录、任意子目录下打开vim自动加载cscope.out
- 一个弱智错误
- xml 生成,解析
- poj(1226)KMP
- Process 执行Linux命令/调用外部程序
- openfire + spark 服务
- vim自动加载模板的插件~
- LIUNX学习记录:软raid
- 最大流最大点权独立集
- JAVA加密和解密案例
- Win7 + Linux Kylin 双系统
- android之Runnable与Thread的区别详解
- Eclipse远程连接Hadoop开发时,提示 ClassNotFoundException
- 【数学】 HDU 1110 Equipment Box
- word2vec中关于霍夫曼树的应用原理