vim创建程序文件自动添加头部注释
1.vim创建程序文件自动添加头部注释
简介
实现写一个脚本的时候添加头部注释,方法可自动注释也可手动注释,方法如下:
一、添加配置文件
[root@testOS ~]# vim ~/.vimrc
配置内容如下:(示例包含shell、Python脚本)
" Adding header comments when creating *. sh or *. py scripts in vim "" Press the shortcut key F8 to automatically add Shell and header comments "" Press the shortcut key F9 to automatically add Python and header comments map <F8> ms:call AddTitleForShell()<cr> ' s " This shell script automatic annotation autocmd BufNewFile *.sh exec ":call AddTitleForShell()" function AddTitleForShell() call append(0,"#!/bin/bash") call append(1,"# *********************************************************#") call append(2,"# * Author : aRUA #") call append(3,"# * Email : xxxxxxxxxxxxxxxxxxxxxx #") call append(4,"# * Create time : ".strftime("%Y-%m-%d %H:%M:%S")." #") call append(5,"# * Filename : ".expand("%:t")." #") call append(6,"# * Description : This is Shell scripts #") call append(7,"# #") call append(8,"# #") call append(9,"# *********************************************************#") endfunction map <F9> ms:call AddTitleForPython()<cr> ' s " This python script automatic annotation autocmd BufNewFile *.py exec ":call AddTitleForPython()" function AddTitleForPython() call append(0,"#!/usr/bin/python3") call append(1,"# *********************************************************#") call append(2,"# * Author : aRUA #") call append(3,"# * Email : xxxxxxxxxxxxxxxxxxxxxx #") call append(4,"# * Create time : ".strftime("%Y-%m-%d %H:%M:%S")." #") call append(5,"# * Filename : ".expand("%:t")." #") call append(6,"# * Description : This is Python scripts #") call append(7,"# #") call append(8,"# #") call append(9,"# *********************************************************#") endfunction
通过删除下面两行可实现F8/F9手动添加注释
autocmd BufNewFile *.sh exec ":call AddTitleForShell()" 或 autocmd BufNewFile *.py exec ":call AddTitleForPython()"
二、测试
示例如下(shell示例):
[root@testOS ~]# vim hello.sh #!/bin/bash # *********************************************************# # * Author : aRUA # # * Email : xxxxxxxxxxxxxxxxxxxxxx # # * Create time : 2023-06-29 23:32:41 # # * Filename : hello.sh # # * Description : This is Shell scripts # # # # # # *********************************************************#
示例如下(python示例):
[root@testOS ~]# vim hello.py #!/usr/bin/python3 # *********************************************************# # * Author : aRUA # # * Email : xxxxxxxxxxxxxxxxxxxxxx # # * Create time : 2023-06-29 23:33:20 # # * Filename : hello.py # # * Description : This is Python scripts # # # # # # *********************************************************#
至此
已经实现成功。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· Apache Tomcat RCE漏洞复现(CVE-2025-24813)