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;
}
相关推荐
学会去珍惜9 小时前
是什么意思c语言
c语言·编程语言·底层开发·面向过程·系统软件
handler0111 小时前
拒绝权限报错!三分钟掌握 Linux 权限管理
linux·c语言·c++·笔记·学习
代码中介商13 小时前
C语言数据存储深度解析:从原码反码补码到浮点数存储
c语言·开发语言·内存
hipolymers13 小时前
C语言怎么样?难学吗?
c语言·数据结构·学习·算法·编程
努力努力再努力wz16 小时前
【Linux网络系列】深入理解 I/O 多路复用:从 select 痛点到 poll 高并发服务器落地,基于 Poll、智能指针与非阻塞 I/O与线程池手写一个高性能 HTTP 服务器!(附源码)
java·linux·运维·服务器·c语言·c++·python
minji...16 小时前
Linux 线程同步与互斥(四) POSIX信号量,基于环形队列的生产者消费者模型
linux·运维·服务器·c语言·开发语言·c++
uElY ITER16 小时前
VS与SQL Sever(C语言操作数据库)
c语言·数据库·sql
程序猿编码17 小时前
给Linux程序穿“隐身衣”——ELF运行时加密器全解析(C/C++代码实现)
linux·c语言·c++·网络安全·elf·内存安全
Goway_Hui17 小时前
【ReactNative鸿蒙化-三方库使用与C-API集成】
c语言·react native·harmonyos