gpt4 book ai didi

c++ - 如何添加为 C++ 生成类源文件和头文件的 vim 命令?

转载 作者:太空狗 更新时间:2023-10-29 12:08:42 26 4
gpt4 key购买 nike

当我从 VIM 命令行 键入 :Class Toto 时,我想获取 header 和源文件及其模板,就像任何编辑器在创建新类(class)。所以如果

输入:Class Toto

然后

输出:

toto.h

#ifndef TOTO_H
#define TOTO_H

class toto
{
public:
toto();
virtual ~toto();
protected:
private:
};

#endif // TOTO_H

toto.cpp

#include "toto.h"

toto::toto()
{
//ctor
}


toto::~toto()
{
//dtor
}

我得到:

./src/toto.c

./include/toto.h

自动生成(有srcinclude文件夹就完美了)

最佳答案

在一个函数下面,我添加到我的 ~/.vimrc 文件中

 "C++ Class Generator                                                                                                    
function! Class(ClassName)
"================== editing header file =====================
let header = a:ClassName.".h"
:vsp %:h/.h
call append(0,"#ifndef ".toupper(a:ClassName)."_H")
call append(1,"#define ".toupper(a:ClassName)."_H")
call append(2," ")
call append(3,"class ".a:ClassName )
call append(4, "{")
call append(5, " public:")
call append(6, " ".a:ClassName."();")
call append(7, " virtual ~".a:ClassName."();")
call append(8, " protected:")
call append(9, " private:")
call append(10, "};")
call append(11,"#endif // ".toupper(a:ClassName)."_H")
:execute 'write' header
"================== editing source file ========================
let src = a:ClassName.".cpp"
:vsp %:h/.cpp
call append(0,"#include ".a:ClassName.".h")
call append(1," ")
call append(2,a:ClassName."::".a:ClassName."()")
call append(3,"{")
call append(4,"//ctor ")
call append(5,"}")
call append(6," ")
call append(7," ")
call append(8,a:ClassName."::~".a:ClassName."()")
call append(9,"{")
call append(10,"//dtor ")
call append(11,"}")
:execute 'write' src
endfunction

open vim and type :call Class("toto")

你的 vim 将被分成 3 部分:

  1. 你当前的文件
  2. toto.h
  3. toto.cpp使用上述模板

if you want to cutomize the command :call Class("toto") to :Class toto add this line in your ~/.vimrc :

command! -nargs=1 Class call Class(<f-args>) 

结果:

enter image description here

关于c++ - 如何添加为 C++ 生成类源文件和头文件的 vim 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57078938/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com