11_vim自动插入文件头模板(.c/.cpp/.py/.txt)设置

vim配置

$ sudo vim ~/.vimrc

bash 复制代码
" ========== Super Template Pro(通用版) ==========

function! SuperTemplate()
    " ---------- 防止重复插入 ----------
    if line('$') > 1 || getline(1) != ''
        return
    endif

    " ---------- 基本信息 ----------
    let filename = expand('%:t')
    let filename_base = expand('%:t:r')
    let ext = expand('%:e')
    let template_dir = expand('~/.vim/templates/')

    " ---------- include guard ----------
    let guard = toupper(substitute(filename_base, '\W', '_', 'g')) . '_H'

    " ---------- 选择模板 ----------
    if ext ==# 'c'
        execute 'silent 0r ' . template_dir . 'skeleton.c'
    elseif ext =~# 'cpp\|cc\|cxx'
        execute 'silent 0r ' . template_dir . 'skeleton.cpp'
    elseif ext ==# 'h'
        execute 'silent 0r ' . template_dir . 'skeleton.h'
    elseif ext ==# 'hpp'
        execute 'silent 0r ' . template_dir . 'skeleton.hpp'
    elseif ext ==# 'py'
        execute 'silent 0r ' . template_dir . 'skeleton.py'
    elseif ext ==# 'txt'
        execute 'silent 0r ' . template_dir . 'skeleton.txt'
    else
        return
    endif

    " ---------- 通用变量替换(必须加 e 防报错) ----------
    silent %s/{FILENAME}/\=filename/ge
    silent %s/{FILENAME_BASE}/\=filename_base/ge
    silent %s/{DATE}/\=strftime("%Y-%m-%d %H:%M")/ge
    silent %s/{AUTHOR}/帅哥/ge
    silent %s/{GUARD}/\=guard/ge

    " ---------- 自动跳到编辑位置 ----------
    if search("Description", "cw")
        normal! f:la
        startinsert
    elseif search("{\s*$", "cw")
        normal! o
        startinsert
    else
        startinsert
    endif
endfunction

autocmd BufNewFile * call SuperTemplate()

自行替换"silent %s/{AUTHOR}/帅哥/ge"中的"帅哥"作为自己的名头。

对应模板文件设置

~$ sudo vim ~/.vim/templates/skeleton.h

c 复制代码
/*
*****************************************************************************
*Name        : {FILENAME}
*Author      : {AUTHOR}
*Created on  : {DATE}
*Description :
***************************************************************************** 
*/

#ifndef {GUARD}
#define {GUARD}

#ifdef __cplusplus
extern "C" {
#endif

// ******************** 包含头文件 ********************


// ******************** 宏定义 ********************


// ******************** 类型定义 ********************


// ******************** 函数声明 ********************



#ifdef __cplusplus
}
#endif

#endif /* {GUARD} */

~$ sudo vim ~/.vim/templates/skeleton.c

c 复制代码
/*
***************************************************************************** 
*Name        : {FILENAME}
*Author      : {AUTHOR}
*Created on  : {DATE}
*Description :
*****************************************************************************  
*/

#include <stdio.h>

int main(int argc, char* argv[])
{
    
    return 0;
}

~$ sudo vim ~/.vim/templates/skeleton.cpp

cpp 复制代码
/*
***************************************************************************** 
*Name        : {FILENAME}
*Author      : {AUTHOR}
*Created on  : {DATE}
*Description :
***************************************************************************** 
*/

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
    cout << "Hello, World!" << endl;

    return 0;
}

~$ sudo vim ~/.vim/templates/skeleton.py

python 复制代码
# **************************************************************************** 
# Name        : {FILENAME}
# Author      : {AUTHOR}
# Created on  : {DATE}
# Description :
# **************************************************************************** 

def main():
    print("Hello, World!")


if __name__ == "__main__":
    main()

~$ sudo vim ~/.vim/templates/skeleton.txt

bash 复制代码
****************************************************************************
Name        : {FILENAME}
Author      : {AUTHOR}
Created on  : {DATE}
Description :
****************************************************************************

到此完成,直接vim test.c查看效果

c 复制代码
/*
***************************************************************************** 
*Name        : a.c
*Author      : 帅哥
*Created on  : 2026-04-22 17:43
*Description :
*****************************************************************************  
*/

#include <stdio.h>

int main(int argc, char* argv[])
{

    return 0;
}
相关推荐
wdfk_prog7 小时前
嵌入式面试真题第 11 题:RTOS 优先级翻转与实时任务阻塞的通用治理
c语言·缓存·面试·职场和发展·架构
无相求码13 小时前
罪魁祸首:for 括号后面那个"隐形分号"
c语言
hy.z_77713 小时前
【C语言】11. 深入理解指针1
c语言·开发语言
ComputerInBook15 小时前
C & C++ 中的关键字 volatile 之详解
c语言·c++·volatile·编译器优化
Soari15 小时前
【突发暴更】Claude Code v2.1.208 :无障碍辅助模式正式上线、Vim 连击热键出逃、大修内存泄漏与跨域 AWS SSO 登录崩溃!
编辑器·vim·aws·claude code
智者知已应修善业16 小时前
【51单片机实现初始化数码管不显示按启动从0秒计时到60按暂停一次显示当前秒按2次恢复原始状态】2024-6-11
c语言·c++·经验分享·笔记·嵌入式硬件·算法·51单片机
小樱花的樱花17 小时前
Linux 多线程编程:互斥锁(Mutex)详解
linux·c语言·开发语言
noipp18 小时前
推荐题目:洛谷 P13554 【MX-X15-T1】奶龙龙
c语言·数据结构·c++·算法·编程·洛谷
aramae20 小时前
C++11:现代C++的里程碑
c语言·开发语言·c++·windows·git·后端
无相求码21 小时前
为什么你的第二个 scanf 还没输入就直接跳过了?C语言输入缓冲区陷阱
c语言·后端