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;
}
相关推荐
zlinear数据采集卡3 小时前
基准电压电路深度解析:从理论参数到ZLinear采集卡的精准参考实战
c语言·单片机·嵌入式硬件·fpga开发·自动化
日晨难再4 小时前
C语言&Python&Bash&Tcl:全局变量和局部变量
c语言·python·bash·tcl
AI科技星6 小时前
基于光速螺旋第一性原理:$G,\varepsilon_0,\alpha$引电统一完整推导+严谨证明+高精度数值全维度分析
c语言·开发语言·网络·量子计算·agi
xgstb6 小时前
C语言随机数生成技巧
c语言·伪随机数·time函数·srand函数·随机数生成
c238567 小时前
c/c++中的二叉树进阶
c语言·c++·算法
0x3F(小茶)7 小时前
嵌入式C设计模式完全指南(基于《C嵌入式编程设计模式》)
c语言·开发语言·单片机·嵌入式硬件·设计模式
189228048618 小时前
NV022固态MT29F16T08GWLCEM5-QBES:C
c语言·开发语言
紫阡星影9 小时前
【STM32CubeMX项目】智能家居门禁系统
c语言·单片机·嵌入式硬件
玖玥拾9 小时前
C/C++ 基础笔记(六)
c语言·c++·内存管理
SoftLipaRZC10 小时前
C语言自定义类型:结构体完全指南
c语言·开发语言