天天看点

VIM 新建文件自动注释配置支持html、php、shell、py、c

VIM 新建文件自动注释html、php、shell、py、c配置支持

    • 前言
    • 解决方案
    • 最后

前言

vim新建文件自动注释在网上找到的大多都是c、sh文件类的注释,而没有提及html和php的注释,下面是我参考网上各位大牛的代码优化出的针对html和php文件创建时自动注释的配置方案。在这里贴出来分享给搭建供大家参考。

解决方案

在~/.vimrc文件中添加如下代码即可

"自动保存最后修改时间
function SetLastModifiedTimes()
	let line = getline(7)
	let newtime = "* @LastEditTime: ".strftime("%Y年%m月%d日 %H时%I分%S秒")
	let repl = substitute(line,".*$",newtime,"g")
	let res = search("@LastEditTime","w")
	if res
		call setline(7,repl)
	endif
endfunction
autocmd BufWrite *.php call SetLastModifiedTimes()


" 当新建 .h .c .hpp .cpp .mk .sh等文件时自动调用SetTitle 函数
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh,*.php,*.html exec ":call SetTitle()" 
" 加入注释 

"加入c cpp h hpp ch mk php 等文件注释
func SetComment()
	call setline(1,"/*================================================================") 
	call append(line("."),   "*   Copyright (C) ".strftime("%Y")." EdisonLiu_ All rights reserved.")
	call append(line(".")+1, "*   ") 
	call append(line(".")+2, "* @Author: 偻儸小卒[EdisonLiu_]") 
	call append(line(".")+3, "* @Date:".strftime("%Y年%m月%d日 %H时%I分%S秒"))
	call append(line(".")+4, "* @LastEditTime:".strftime("%Y年%m月%d日 %H时%I分%S秒"))
	call append(line(".")+5, "* @Description:") 
	call append(line(".")+6, "*")
	call append(line(".")+7, "================================================================*/") 
	call append(line(".")+8, "")
	call append(line(".")+9, "")
endfunc

"加入html注释
func SetHtmlComment()
	call setline(1,"<!--") 
	call append(line("."),   " Copyright (C) ".strftime("%Y")." EdisonLiu_ All rights reserved.")
	call append(line(".")+1, "   ") 
	call append(line(".")+2, " @Author: 偻儸小卒[EdisonLiu_] [email protected]") 
	call append(line(".")+3, " @Date:".strftime("%Y年%m月%d日 %H时%I分%S秒"))
	call append(line(".")+4, " @LastEditTime:".strftime("%Y年%m月%d日 %H时%I分%S秒"))
	call append(line(".")+5, " @Description:") 
	call append(line(".")+6, "")
	call append(line(".")+7, "-->") 
	call append(line(".")+8, "")
	call append(line(".")+9, "")
endfunc


" 加入sh,Makefile注释
func SetComment_sh()
	call setline(3, "#================================================================") 
	call setline(4, "#   Copyright (C) ".strftime("%Y")." EdisonLiu_ All rights reserved.")
	call setline(5, "#   ") 
	call setline(6, "#   @Author: 偻儸小卒[EdisonLiu_] [email protected]") 
	call setline(7, "#   @Date:".strftime("%Y年%m月%d日 %H时%I分%S秒"))
	call setline(8, "#   @LastEditTime:".strftime("%Y年%m月%d日 %H时%I分%S秒")) 
	call setline(9, "#   @Description:") 
	call setline(10, "#")
	call setline(11, "#================================================================")
	call setline(12, "")
	call setline(13, "")
endfunc 
" 定义函数SetTitle,自动插入文件头 
func SetTitle()
	if &filetype == 'make' 
		call setline(1,"") 
		call setline(2,"")
		call SetComment_sh()
 
	elseif &filetype == 'sh' 
		call setline(1,"#!/system/bin/sh") 
		call setline(2,"")
		call SetComment_sh()
	elseif &filetype == 'html'
		call setline(2,"")
		call SetHtmlComment()
	else
	     call SetComment()
	     if expand("%:e") == 'hpp' 
		  call append(line(".")+10, "#ifndef _".toupper(expand("%:t:r"))."_H") 
		  call append(line(".")+11, "#define _".toupper(expand("%:t:r"))."_H") 
		  call append(line(".")+12, "#ifdef __cplusplus") 
		  call append(line(".")+13, "extern \"C\"") 
		  call append(line(".")+14, "{") 
		  call append(line(".")+15, "#endif") 
		  call append(line(".")+16, "") 
		  call append(line(".")+17, "#ifdef __cplusplus") 
		  call append(line(".")+18, "}") 
		  call append(line(".")+19, "#endif") 
		  call append(line(".")+20, "#endif //".toupper(expand("%:t:r"))."_H") 
 
	     elseif expand("%:e") == 'h' 
	  	call append(line(".")+10, "#pragma once") 
	     elseif &filetype == 'c' 
	  	call append(line(".")+10,"#include \"".expand("%:t:r").".h\"") 
	     elseif &filetype == 'cpp' 
	  	call append(line(".")+10, "#include \"".expand("%:t:r").".h\"") 
             elseif expand("%:e") == 'php' 
		call setline(1,"<?php  ")
		call append(line("."),"/*================================================================")
	  	call append(line(".")+11, " ?>")
	     endif
	endif
endfunc

           

最后

VIM实现php变量选中点击查看

vim快速折叠html点击查看

更多vim配置请看本人vimrc配置文件git仓库vimrc配置

继续阅读