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;
}