一个Vim脚本建立文件时添加头

VIM创建文件时添加文件注释头,修改时更新最后修改时间和最小一位版本号。支持文件类型(.sh .py .pl .vim .java .c .h .cpp .cc .h .js)

C/C++ 风格注释文件头

1
2
3
4
5
6
7
8
/*
* C source file
*
* $id: test.c,v 1.0.0 2008/12/25 05:03:57 master Exp $
* Copyright (C) 2008 Yunlong Wen ([email protected])
*
* Last Modified: 2008/12/25 05:03:57
*/


Bash 风格注释文件头

1
2
3
4
5
6
#!/bin/bash
#
# $id: test.sh,v 1.0.0 2008/12/25 05:04:30 master Exp $
# Copyright (C) 2008 Yunlong Wen ([email protected])
#
# Last Modified: 2008/12/25 05:04:30

下载文件放在 vim 的 plugin 目录即可
Yunlong Wen ([email protected]) 需要定义变量 AUTHOR

Bash 下修改 .bashrc 添加
AUTHOR=Yunlong Wen ([email protected])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
" Vim Script File
"
" $id: x.vim, v1.0.0 2008/06/22 03:47:20 motse Exp $
" Copyright (C) 2011
"
" Last Modified: 2011/06/22 10:47:45
"
" vim:tw=80:sw=4:ts=4
"
 
function s:filetype ()
 
  let s:file = expand("<afile>:t")
  if match (s:file, "\.sh$") != -1
    let s:comment = "#"
    let s:type = s:comment . "!" . system ("whereis -b bash | awk '{print $2}' | tr -d '\n'")
  elseif match (s:file, "\.py$") != -1
    let s:comment = "#"
    let s:type = s:comment . "!" . system ("whereis -b python | awk '{print $2}' | tr -d '\n'")
  elseif match (s:file, "\.pl$") != -1
    let s:comment = "#"
    let s:type = s:comment . "!" . system ("whereis -b perl | awk '{print $2}' | tr -d '\n'")
  elseif match (s:file, "\.vim$") != -1
    let s:comment = "\""
    let s:type = s:comment . " Vim script file"
  elseif match (s:file, "\.java$") != -1
	let s:comment = " *"
	let s:type = s:comment . " Java source file"
  elseif match (s:file, "\.c$") != -1
	let s:comment = " *"
	let s:type = s:comment . " C source file"
  elseif match (s:file, "\.h$") != -1
	let s:comment = " *"
	let s:type = s:comment . " C/C++ header file"
  elseif match (s:file, "\.cpp$") != -1
	let s:comment = " *"
	let s:type = s:comment . " C++ source file"
  elseif match (s:file, "\.cc$") != -1
	let s:comment = " *"
	let s:type = s:comment . " C/C++ source file"
  elseif match (s:file, "\.js") != -1
	let s:comment = " *"
	let s:type = s:comment . " JavaScript source file"
  else
    let s:comment = ''
  endif
  unlet s:file
 
endfunction
 
function s:insert ()
 
  call s:filetype ()
  if s:comment == ''
      return
  endif
 
  let s:author = $AUTHOR
  if s:author == ''
      s:author = !whoami
  endif
 
  let s:id = s:comment . " $id: " . expand("<afile>") . ", v1.0.0 " . strftime("%Y/%m/%d %H:%M:%S") . " " . system ("id -un | tr -d '\n'") . " Exp $"
  let s:copyright = s:comment . " Copyright (C) " . strftime("%Y") . ' ' . s:author
  let s:modified = s:comment . " Last Modified: " . strftime ("%Y/%m/%d %H:%M:%S")
 
  call append (0, s:type)
  call append (1, s:comment)
  call append (2, s:id)
  call append (3, s:copyright)
  call append (4, s:comment)
  call append (5, s:modified)
  call append (6, s:comment)
  call append (7, s:comment." vim:tw=80:sw=4:ts=4")
  call append (8, s:comment)
 
  if s:comment == " *"
    call append (0, "/*")
	call append (10, " */")
 
    call append (12, "")
    call append (13, "")
    call cursor (13, 1)
  else
    call append (10, "")
    call append (11, "")
    call cursor (11, 1)
  endif
 
  unlet s:comment
  unlet s:type
  unlet s:id
  unlet s:copyright
  unlet s:modified
  unlet s:author
 
endfunction
 
function s:update ()
 
  call s:filetype ()
  if s:type == ''
      return
  endif
 
  let s:pattern = s:comment . " Last Modified: [0-9/:]+ [0-9]+"
 
  if s:comment == " *"
  	let s:ln = 7
  else
	let s:ln = 6
  endif
 
  let s:line = getline (s:ln)
  if match (s:line, s:pattern) != -1
    let s:modified = s:comment . " Last Modified: " . strftime ("%Y/%m/%d %H:%M:%S")
    call setline (s:ln, s:modified)
    unlet s:modified
  endif
 
  let s:line = getline(4) 
  if match (s:line, 'v\d\+.\d\+.\d\+') != -1
    let s:fstr = 'id: '.expand("<afile>:t")
    let s:vstr = matchstr(s:line, 'v\d\+.\d\+.\d\+')
    let s:msm = split(s:vstr, '\.')
    let s:vstr = s:msm[0].'.'.s:msm[1].'.'.(s:msm[2]+1)
 
    let s:line = substitute(s:line, 'id: [^,]\+', s:fstr, "")
    let s:line = substitute(s:line, 'v\d\+.\d\+.\d\+', s:vstr, "")
    call setline(4, s:line)
    unlet s:vstr
    unlet s:msm
    unlet s:fstr
  endif
 
  unlet s:comment
  unlet s:pattern
  unlet s:ln
  unlet s:line
 
endfunction
 
autocmd BufNewFile * call s:insert ()
autocmd BufWritePre * call s:update ()

下载代码:header.vim


Leave a comment

Your comment