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上附件的插入位置。。

贴下代码吧,不是很长,而且很清晰

[cpp] view plaincopy
  1. "=============================================================================  
  2. " Vim global plugin for load template files  
  3. " File:         template_loader.vim  
  4. " Maintainer:   Jestery Liu <jestery@gmail.com>  
  5. " Last Change:  2007/02/02  
  6. " Version:      0.1  
  7. " Description:   
  8. "   Load template file for editing new files  
  9. " Options:  
  10. "   (int) g:template_load  
  11. "       Set to 0 to disable template auto-loading, 1 for enable.  
  12. "  
  13. "   (int) g:template_confirm  
  14. "       If set to 1, everytime you edit a new file you will be asked for  
  15. "       whether load template or not.  
  16. "  
  17. "   (string) g:template_path  
  18. "       If set, script will look for template files in this path, if not set,  
  19. "       the script will locate template files in each path in &runtimepath.  
  20. "  
  21. "   (string) g:template_dir_name  
  22. "       By defaultif g:template_path is not set, script will locate template  
  23. "       files in &runtimepath.'/templates/''~/.vim/templates/' for example,  
  24. "       you can custom your own directory name by set this value. If  
  25. "       g:template_path is set, this value never be used.  
  26. "  
  27. "   (string) g:template_prefix  
  28. "       The default template file name is 'tpl.extension'for example, if you  
  29. "       create a 'foo.c''~/.vim/templates/tpl.c' will be loaded. You can set  
  30. "       this value to 'skel' to load 'skel.c'  
  31. "  
  32. "   (int) g:template_tags_replacing  
  33. "       If set to 1, some pre-defined tags will be replaced by the  
  34. "       following variables:  
  35. "           (string) g:T_AUTHOR  
  36. "           (string) g:T_AUTHOR_EMAIL  
  37. "           (string) g:T_AUTHOR_WEBSITE  
  38. "           (string) g:T_LICENSE  
  39. "           (string) g:T_DATE_FORMAT (Same as strftime)  
  40. "           (int)   g:T_FILENAME_USE_FULL_PATH  
  41. "                   (If 1, /foo/bar.c will be used, otherwise, bar.c)  
  42. "       Pre-defined tags:  
  43. "           <T_AUTHOR>, <T_AUTHOR_EMAIL>, <T_AUTHOR_WEBSITE>  
  44. "           <T_LICENSE>, <T_FILENAME>, <T_CREATE_DATE>  
  45. "           <T_CURSOR>  
  46. "           If found <T_CURSOR>, the cursor will be placed here and <T_CURSOR>  
  47. "           will be deleted.  
  48. "  
  49. "   (int) g:template_replace_start_line  
  50. "       Start tag replacing from here. Default is 1  
  51. "  
  52. "   (int) g:template_replace_end_line  
  53. "       End tag replacing to here. Default is the last line ("$")  
  54. if exists("g:template_load") && g:template_load==1  
  55.     augroup Template_Loader  
  56.         autocmd!  
  57.         au BufNewFile * call LoadTemplate()  
  58.     augroup END  
  59. else  
  60.     finish  
  61. endif  
  62. function! LoadTemplate()  
  63.     let s:template_file = ""  
  64.     let g:template_prefix = exists("g:template_prefix") ? g:template_prefix : 'tpl'  
  65.     let s:ext = expand('%:e')  
  66.     if s:ext == ""  
  67.         let s:ext = expand('%:t')  
  68.     endif  
  69.     let s:template_file_name = g:template_prefix . '.' . s:ext  
  70.     if exists("g:template_path")  
  71.         let s:template_file = g:template_path.'/'.s:template_file_name  
  72.     else  
  73.         let s:template_dir_name = exists("g:template_dir_name") ? g:template_dir_name : 'templates'  
  74.         let s:rtp = &runtimepath  
  75.         let s:dirs = split(s:rtp, ',')  
  76.         if empty(s:dirs)  
  77.             return  
  78.         endif  
  79.         for dir in s:dirs  
  80.             let s:template_file = dir.'/'.s:template_dir_name.'/'.s:template_file_name  
  81.             if filereadable(s:template_file)  
  82.                 break  
  83.             endif  
  84.         endfor  
  85.         "unlet s:rtp, s:dirs, s:template_dir_name  
  86.     endif  
  87.     if s:template_file=="" || !filereadable(s:template_file)  
  88.         echo "Missing template file. (".s:template_file.")"  
  89.         return  
  90.     endif  
  91.     call LoadTemplateFile(s:template_file)  
  92. endfunction  
  93. function! LoadTemplateFile(filename)  
  94.     let choice = 1  
  95.     if exists("g:template_confirm") && g:template_confirm==1  
  96.         let choice = confirm("Do you want to load template for this new file?""&Yes/n&No")  
  97.     endif  
  98.     if choice==0  
  99.         return  
  100.     endif  
  101.     sil! execute "0r " . a:filename  
  102.     call TemplateReplTags()  
  103. endfunction  
  104. function! TemplateReplTags()  
  105.     if g:template_tags_replacing != 1  
  106.         return  
  107.     endif  
  108.     let sl = exists("g:template_replace_start_line") ? g:template_replace_start_line : 1  
  109.     let el = exists("g:template_replace_end_line") ? g:template_replace_end_line : "$"  
  110.     if exists("g:T_AUTHOR")  
  111.         sil! execute sl.','.el."s/<T_AUTHOR>/".g:T_AUTHOR."/g"  
  112.     endif  
  113.     if exists("g:T_AUTHOR_EMAIL")  
  114.         sil! execute sl.','.el."s/<T_AUTHOR_EMAIL>/".g:T_AUTHOR_EMAIL."/g"  
  115.     endif  
  116.     if exists("g:T_AUTHOR_WEBSITE")  
  117.         sil! execute sl.','.el."s=<T_AUTHOR_WEBSITE>=".g:T_AUTHOR_WEBSITE."=g"  
  118.     endif  
  119.     if exists("g:T_LICENSE")  
  120.         sil! execute sl.','.el."s/<T_LICENSE>/".g:T_LICENSE."/g"  
  121.     endif  
  122.     if exists("g:T_DATE_FORMAT")   
  123.         sil! execute sl.','.el."s/<T_CREATE_DATE>/".strftime(g:T_DATE_FORMAT)."/g"  
  124.     endif  
  125.     if exists("g:T_FILENAME_USE_FULL_PATH") && g:T_FILENAME_USE_FULL_PATH==1  
  126.         let s:fn = expand("%:p")  
  127.     else  
  128.         let s:fn = expand("%:t")  
  129.     endif  
  130.     sil! execute sl.','.el."s/<T_FILENAME>/".s:fn."/g"  
  131.     unlet s:fn  
  132.     let s:curpos = search("<T_CURSOR>"'W')  
  133.     if !empty(s:curpos)  
  134.         call cursor(s:curpos)  
  135.         sil! execute "normal df>"  
  136.         sil! execute "startinsert"  
  137.     endif  
  138. endfunction  
  139. " vim: set ts=4 sw=4 tw=78:  
 

0 0
原创粉丝点击