vim文件头配置

很多时候希望写代码的时候Vim能自动添加文件头,显示一些注释比如作者、创建时间、修改时间等等信息。事前先百度,参考了一些教程之后实际动手操作了一番,记录下我在用的代码。

修改 /etc/vimrc 文件,在末尾添加一下代码

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
" 使用<F4>作为触发条件
map <F4> :call TitleDet()<cr>'s

“ 添加文件头
function AddTitle()
call append(0,"/* ")
call append(1," * Author : Haoojie")
call append(2," * Create time : ".strftime("%Y-%m-%d %H:%M"))
call append(3," * Last modified : ".strftime("%Y-%m-%d %H:%M"))
call append(4," * Filename : ".expand("%:t"))
call append(5," * Description : ")
call append(6," */")
call append(7,"")
echohl WarningMsg | echo "Successful in adding the copyright." | echohl None
endf

“ 更新文件时间和描述
function UpdateTitle()
normal m'
execute '/ * Last modified :/s@:.*$@\=strftime(": %Y-%m-%d %H:%M")@'
normal ''
normal mk
execute '/ * Filename :/s@:.*$@\=": ".expand("%:t")@'
execute "noh"
normal 'k
echohl WarningMsg | echo "Successful in updating the copy right." | echohl None
endfunction

“ 主函数
“ 如果前8行有 Last modified,更新文件头
“ 如果前8行没有 Last modified,添加文件头
function TitleDet()
let n=1
while n < 8
let line = getline(n)
if line =~ '^\s\*\sLast\smodified\s:\s*.*$'
call UpdateTitle()
return
endif
let n = n + 1
endwhile
call AddTitle()
endfunction

参考链接

VIM 一键自动添加文件头注释

0%