作为一个进场在Linux系统上写shell脚本的开发人员, 对每个shell脚本都要编写重复的头部信息.这是一个很麻烦的事情,所以,有什么办法,在新建.sh
文件的时候自动生成头部注释信息吗??
当然是有的. 一下介绍几种方式.都是可以使用的
autocmd BufNewFile *.sh exec ":call AddTitleForShell()"
function AddTitleForShell()
call append(0,"#!/bin/bash")
call append(1,"# **********************************************************")
call append(2,"# * Author : Dreamhai")
call append(3,"# * Email : xxxxx@163.com")
call append(4,"# * Create time : ".strftime("%Y-%m-%d %H:%M"))
call append(5,"# * Filename : ".expand("%:t"))
call append(6,"# * Description : ")
call append(7,"# **********************************************************")
endfunction
查看下vimrc
文件
测试: 随意新建一个.sh
文件
map <F4> :call TitleDet()<cr>
function AddTitle()
call append(0,"\#!/bin/bash")
call append(1,"# ******************************************************")
call append(2,"# * Author : Dreamhai")
call append(3,"# * Email : xxxxx@163.com")
call append(4,"# * Create time : ".strftime("%Y-%m-%d %H:%M"))
call append(5,"# * Filename : ".expand("%:t"))
call append(6,"# * Description : ")
call append(7,"# ******************************************************")
echohl WarningMsg | echo "Successful in adding copyright." | echohl None
endf
function UpdateTitle()
normal m'
execute '/# Last modified/s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M")@'
normal ''
normal mk
execute '/# Filename/s@:.*$@\=":\t".expand("%:t")@'
execute "noh"
normal 'k
echohl WarningMsg | echo "Successful in updating the copyright." | echohl None
endfunction
function TitleDet()
let n=1
while n < 10
let line = getline(n)
if line =~ '^\#\s*\S*Last\smodified\S*.*$'
call UpdateTitle()
return
endif
let n = n + 1
endwhile
call AddTitle()
endfunction
注意:mac下,写脚本vim一个文件以后,可能需要在非编辑模式下按fn+f4才会在脚本开头生成头部注释信息。
F4
快捷键进行自动插入效果不演示啦
参考:
因篇幅问题不能全部显示,请点此查看更多更全内容