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;
}
相关推荐
(Charon)31 分钟前
【C++】锁与原子操作(四):自旋锁的完整实现与性能优化
c语言·开发语言·c++
FeelTouch Labs1 小时前
Node.js 中的 spawn 和 exec:子进程执行的不同方式对比
node.js·编辑器·vue·vim
小羊先生car2 小时前
RTOS-F429-HAL-Freertos内存管理(2026/8/5)
c语言·单片机·rtos
呜喵王阿尔萨斯2 小时前
extern “C“ in C++
c语言·c++·算法
萌新源5 小时前
电赛C题:我用星闪+UWB做了一个数字钥匙门锁系统,开源了
c语言·华为·开源
不正经学生6 小时前
C语言指针进阶:const 和野指针——给指针加锁,向野指针宣战
c语言·开发语言·c++·算法·c#
多弗朗皮卡丘8 小时前
C语言梦开始的地方3
c语言·开发语言
天空'之城18 小时前
C 语言工业级通用组件手写 24:互补滤波
c语言·开发语言·互补滤波·嵌入式算法·工业级组件
stevenseahang1 天前
C语言标准演化史:从K&R到GNU,谁才是正统?
c语言·gnu·可移植性·标准演化·ansic
别动我齐刘海1 天前
Day6 unitree_G1人形机器人GMR—— MotionInput
c语言·c++·人工智能·学习·机器学习·机器人·github