1. 程式人生 > >vim自動載入模板的外掛~

vim自動載入模板的外掛~

又搜了下,貌似有個CVIM的外掛也很N~~~學習了~~

記得之前實習的時候新建一個檔案.h  .c  .cpp後會自動加一些檔案說明,比如檔案的名字,建立日期、建立者等等一些資訊,想起來使用很方便,就自己找了下怎麼做,沒想到這麼難搜,不過還是被我找到了~~~

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 = "

[email protected]"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上附件的插入位置。。

貼下程式碼吧,不是很長,而且很清晰

  1. "=============================================================================  
  2. " Vim global plugin for load template files  
  3. " File:         template_loader.vim  
  4. " Maintainer:   Jestery Liu <[email protected]>  
  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 fortemplate 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 defaulttemplate 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:  
 

相關推薦

vim自動載入模板外掛~

又搜了下,貌似有個CVIM的外掛也很N~~~學習了~~ 記得之前實習的時候新建一個檔案.h  .c  .cpp後會自動加一些檔案說明,比如檔案的名字,建立日期、建立者等等一些資訊,想起來使用很方便,就自己找了下怎麼做,沒想到這麼難搜,不過還是被我找到了~~~ 1

vim自動載入cscope.out

    cscope可以查詢程式碼的引用、定義等,但是當用vim直接開啟專案子目錄中的一個檔案時,cscope.out不能直接載入。在網上搜了一把,autoload_cscope外掛可以解決這個問題,但是這個外掛只針對c, h檔案,開啟cpp時好像不能生效(可能是我沒找到設

CentOS7 Vim自動補全外掛----YouCompleteMe安裝與配置

最近剛裝了新系統CentOS7,想要把編碼環境配置一下,使用Vim編寫程式少不了使用自動補全外掛,我以前用的是neocomplcache+code_complete+omnicppcomplete。但在網上搜索時,看到了YouCompleteMe,說YCM更好用一些。

vim 自動提示、自動補齊外掛 YouCompleteMe for windows Gvim 安裝及使用效果

YouCompleteMe is a fast, as-you-type, fuzzy-search(親~~支援模糊匹配哦) code completion engine for Vim. It has two completion engines: an identifier-based engi

Vim為特定檔案載入模板

程式設計開發實際上有許多重複性的工作,比如編寫C/C++時有一些標頭檔案是通用的,但在Vim裡面每次建立新檔案預設都是空白檔案,手工輸入其實挺繁瑣的,下面介紹兩種為特定檔案載入模板的方法,僅以C為例,其他語言類似 方法一 這種方法的特點是以純vimscript實現,跨平臺 a

vim python自動補全外掛:pydiction

vim python自動補全外掛:pydiction 可以實現下面python程式碼的自動補全: 1.簡單python關鍵詞補全 2.python 函式補全帶括號 3.python 模組補全 4.python 模組內函式,變數補全 5.from module

pace.js – 網頁自動載入進度條外掛

之前有很多同學詢問覺唯網站頂部的頁面載入進度條是怎麼實現的,頁面的載入進度百分比,有時候獲取是比較麻煩的,當然也可以利用一些優秀的JavaScript外掛來實現,今天就為大家介紹這樣子的一款外掛:pace.js。 在頁面中引入Pace.js,頁面就會自動監測你的請求

CAD.net 寫登錄檔自動載入外掛dll

RegistryKey LocaIMachine = Registry.LocalMachine; RegistryKey MyPrograrm = LocaIMach

vim ctags cscope lookupfile外掛(包括如何自動生成索引tag、自動查詢索引tag)

  好長時間沒有上來更新了, 今天趁老闆不再上來休閒一下. 本章要說的是和vim的tags相關的內容. 之所以在跳轉之後就說明tags是因為這個功能相當的重要和實用. 好的東西自然是需要提前分享的.   首先, 要說的是關於vim使用ctags, cscope的相關教程

自動補全外掛 jedi-vim

安裝方式 1.  在.vimrc 新增 jedi-vim 和 supertab call vundle#begin() ... Bundle 'davidhalter/jedi-vim' Bundle 'ervandew/supertab' ... call vun

vim安裝youcompleteme自動補全外掛

本文介紹的是如何在vim中安裝youcompleteme自動補全外掛 一、安裝youcompleteme 開啟檔案~/.vimrc 新增下面一行程式碼 Bundle 'Valloric/YouCompleteMe' 儲存退出,開啟一個vim新檔

vim 新建檔案後自動插入模板

例如 vim a.php 自從插入一段註釋 開啟 ~/.vimrc 加上下面程式碼 function AddFileInformation() let infor = "<?php\n" \."/************************

Vim自動載入cscope.out

Vimer初成長,Vim + ctags + cscope 這個組合基本是每個Vimer的必備吧。雖然ctags已經足夠強大,但是cscope可以做的更多。下面來分享下自己的vimrc指令碼關於cscope的一部分,該指令碼可以實現在專案的任一子目錄下,自動的向上查詢cs

實踐中學習vimvim配置檔案、外掛檔案載入路徑

0 引言 理解vim的啟動過程對於增強使用vim的信心非常重要,本文所有的資訊均來自vim自身提供的參考手冊和作者實際操作實踐。VIM REFERENCE MANUAL的Starting Vim這節詳細描述了vim的啟動過程。vim完整的啟動過程非常複雜,因為要相容不同的平

php設計模式--命名空間與自動載入

使用 命令 說明 自動載入 str space () 就會 sta 關於命名空間:   最早的php是沒有命名空間的概念的,這樣不能存在相同名稱的類或者函數,當項目變大了之後,產生沖突的可能性就高了,代碼量也會變大,為了規劃,從php5.3開始對命名空間就支持了。 說明代碼

vim 自動在操作符 前後加上空格 C語言

空格 func 再次 不知道 編碼 placement 字符 cti spa function! Align_Space() let current_line = getline(‘.‘) let replacement = subs

VIM自動補齊括號和引號

VIM補齊括號 VIM補齊引號 在~/.vimrc文件中追加以下內容: inoremap ( ()<ESC>iinoremap [ []<ESC>iinoremap { {}<ESC>iinoremap < <><ESC>iinorem

牛客小白月賽6 G 指紋鎖 set的自動排序 模板

加密 的人 算法 esc code 超過 fin 代碼 sync 鏈接:https://www.nowcoder.com/acm/contest/136/G來源:牛客網 題目描述 HA實驗有一套非常嚴密的安全保障體系,在HA實驗基地的大門,有一個指紋鎖。

Pycharm載入第三方外掛失敗方法(關鍵字:Nothing to show)和安裝失敗解決辦法

一、修復外掛顯示錯誤,共3個步驟: 1,新增額外源:  清華:https://pypi.tuna.tsinghua.edu.cn/simple 阿里雲:http://mirrors.aliyun.com/pypi/simple/ 中國科技大學 https://pypi.mi

PHP規範PSR4(自動載入)介紹

本文件中的關鍵詞“必須”,“必須”,“必需”,“應該”,“不應該”,“應該”,“不應該”,“推薦”,“可以”和“可選”按照RFC 2119中的描述進行解釋。 1 概述 此PSR描述了從檔案路徑自動載入類的規範。它完全可互操作,除了包括PSR-0在內的任何其他自動載入規範外,還可以使用它。此P