Linux中用vim编辑器打开时自动补入头文件
我们在新建一定类型的文件时,可以自动在文件开头加上注释,根据新建文件类型的不同,在文件开头加上不同的注释;
为了实现这个目标,我们需要在~/.vimrc中设定,设定实例如下:
" 自动调用创建文件头
autocmd BufNewFile *.sh, *.csh, *.mk, Makefile, *.v, *.sv, *.py, *.tcl exec ":call SetTitleComment()"
" 设置文件头
func SetTitleComment()
if expand('%:e') == 'v' || expand('%:e') == 'sv'
call setline(1, '// ======================================== //')
call append(line('.'), '// CopyRight(C) '.strftime('%Y').' Turbo Snail')
call append(line('.')+1, '// ALL RIGHTS RESERVED.')
call append(line('.')+2, '// FileName : '.expand('%:t'))
call append(line('.')+3, '// Author : ZeGa')
call append(line('.')+4, '// Email : xxxx.com')
call append(line('.')+5, '// Created : '.strftime('%Y-%m-%d'))
call append(line('.')+6, '// History : v1.0')
call append(line('.')+7, '// ======================================= //')
call append(line('.')+8, '')
call append(line('.')+9, 'module '.toupper(expand('%:t:r')).' ();')
call append(line('.')+10, 'endmodule')
call append(line('.')+11, '')
else
let lnum = 1
if &filetype == 'make'
call setline(1, '#!/bin/make')
elseif expand('%:e') == 'sh'
call setline(1, '#!/bin/bash -f')
elseif expand('%:e') == 'csh'
call setline(1, '#!/bin/csh -f')
elseif expand('%:e') == 'py'
call setline(1, '#!/usr/bin/python')
call setline(2, '# -*- coding: utf-8 -*-')
let lnum = 2
elseif expand('%:e') == 'tcl'
call setline(1, '#!/usr/bin/tclsh')
endif
call append(lnum, '# ========================================= #')
call append(lnum+1, '# CopyRight(C) '.strftime('%Y').' Turbo Snail')
call append(lnum+2, '# ALL RIGHTS RESERVED.')
call append(lnum+3, '# FileName : '.expand('%:t'))
call append(lnum+4, '# Author : ZeGa')
call append(lnum+5, '# Email : xxxx.com')
call append(lnum+6, '# Created : '.strftime('%Y-%m-%d'))
call append(lnum+7, '# History : v1.0')
call append(lnum+8, '# ======================================== #')
call append(lnum+9, '')
endif
endfunc
autocmd BufNewFile *.py,*.sh exec ":call SetTitle()"
""定义函数SetTitle,自动插入文件头
func SetTitle()
"如果文件类型为.sh文件
if &filetype == 'sh'
call setline(1,"\#########################################################################")
call append(line("."), "\# File Name: ".expand("%"))
call append(line(".")+1, "\# Author: xxxxxxx")
call append(line(".")+2, "\# mail: xxxxxxxx@gmail.com")
call append(line(".")+3, "\# Created Time: ".strftime("%c"))
call append(line(".")+4, "\######################################################################$
call append(line(".")+5, "\#!/bin/bash")
call append(line(".")+6, "")
elseif &filetype == 'python'
call setline(1, "\#######################################")
call append(line("."), "\# > File Name: ".expand("%"))
call append(line(".")+1, "\# > Author: xxxxxxxxx")
call append(line(".")+2, " \# > Mail: xxxxxxxx@gmail.com ")
call append(line(".")+3, " \# > Created Time: ".strftime("%c"))
call append(line(".")+4, " \######################################################)
call append(line(".")+5, "#!/usr/bin/python")
call append(line(".")+6, "# -*- coding:utf-8 -*-")
call append(line(".")+7,"")
else
call setline(1, "/*************************************************************************")
call append(line("."), " > File Name: ".expand("%"))
call append(line(".")+1, " > Author: xxxxx")
call append(line(".")+2, " > Mail: xxxxxxxx@gmail.com ")
call append(line(".")+3, " > Created Time: ".strftime("%c"))
call append(line(".")+4, " **********************************************************************/
call append(line(".")+5, "")
endif
"新建文件后,自动定位到文件末尾
autocmd BufNewFile * normal G
endfunc
浙公网安备 33010602011771号