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;
}
相关推荐
qq_589666055 小时前
C语言、C++与C#的区别详解
c语言·c++·c#
昌原的儿子LEO9 小时前
Linux进程知识点总结
linux·服务器·c语言·数据库
一直C10 小时前
【Linux应用编程】深入理解Linux多任务机制:进程原理、状态转换与进程控制实战
linux·开发语言·算法·ubuntu·vim·visual studio code
多弗朗皮卡丘12 小时前
C语言梦开始的地方16:结构体
c语言·开发语言·windows
2401_8628808212 小时前
Linux应用层开发 --- 多任务
linux·服务器·c语言
心仪久了会沦陷12 小时前
数据结构入门系列——顺序表详解:概念、分类与动态实现
c语言·数据结构·经验分享·笔记·开源
witton12 小时前
xmake中将proto文件生成C/C++文件然后再编译链接进项目
c语言·c++·mingw·protobuf·xmake·protobuf-c·protoc-gen-c
dear_bi_MyOnly13 小时前
C语言控制语句与循环逻辑精讲
c语言·开发语言·学习·分类
M78佐菲1 天前
c语言学习笔记:排序与查找方法整理
linux·c语言·笔记·学习·算法
wWYy.1 天前
C++与C的区别
c语言·c++