微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

vim创建程序文件自动添加头部注释

修改 ~/.vimrc,在文件最后添加以下内容


" add header comments for .h .c .hpp .cpp .mk .sh new file
" auto call SetTitle func
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh,*.py exec ":call SetTitle()"

" add comment for cpp
func SetComment_ch()
	call setline(1,"/*================================================================")
	call append(line("."),"*   copyright (C) ".strftime("%Y")." * Ltd. All rights reserved.")
	call append(line(".")+1,"*   ")
	call append(line(".")+2,"*   File name   : ".expand("%:t"))
	call append(line(".")+3,"*   Author      : longbin")
	call append(line(".")+4,"*   Created date: ".strftime("%F %T"))
	call append(line(".")+5,"*   Description : ")
	call append(line(".")+6,"*")
	call append(line(".")+7,"*===============================================================*/")
	call append(line(".")+8,"")
	call append(line(".")+9,"")
endfunc

" add comment for shell,Makefile
func SetComment_sh()
	call setline(3,"#================================================================")
	call setline(4,"#   copyright (C) ".strftime("%Y")." * Ltd. All rights reserved.")
	call setline(5,"#   ")
	call setline(6,"#   File name   : ".expand("%:t"))
	call setline(7,"#   Author      : longbin")
	call setline(8,"#   Created date: ".strftime("%F %T"))
	call setline(9,"#   Description : ")
	call setline(10,"#")
	call setline(11,"#================================================================")
	call setline(12,"")
	call setline(13,"")
endfunc

" SetTitle func,add comment
func SetTitle()
	if &filetype == 'make'
		call setline(1,"")
		call setline(2,"")
		call SetComment_sh()

	elseif &filetype == 'sh'
		call setline(1,"#! /bin/bash")
		call setline(2,"")
		call SetComment_sh()

	elseif &filetype == 'python'
		call setline(1,"#! /usr/bin/env python")
		call setline(2,"# coding=utf-8")
		call setline(3,"")
		call SetComment_sh()

	else
		call SetComment_ch()
		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\"")

		endif

	endif
endfunc

原文地址:https://www.jb51.cc/vim/390111.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐