When I type :Class Toto
from VIM command line, I want to get the header and the source file with their templates like what any editor do when we create a new class. So if
Input
:Class Toto
then
Output:
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
}
I get:
./src/toto.c
./include/toto.h
generated automatically (with src
and include
folders will be perfect)
below a function, i added to my ~/.vimrc
file
"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")
your vim will be splited to 3 parts:
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>)
result :
This is a slight variation of the work of Saif Faidi.
The only thing it brings:
"C++ CLASS GENERATOR: OPENING 2 NEW FILES
function! ClassNew(ClassName)
"================== editing source file =================================
execute "vsp %:h/" . a:ClassName . ".class.cpp"
"At this stage the autocomands for this filetype are done.
" example: inserting the header, and the ifndef... Then:
:execute "normal! a#include \"" . a:ClassName . ".class.hpp\"\<cr>\<cr>"
:execute "normal! a" . a:ClassName . "::" . a:ClassName ."(void)\<cr>{\<cr>"
:execute "normal! a\<tab>return ;\<cr>"
:execute "normal! a}\<cr>\<cr>"
:execute "normal! a" . a:ClassName . "::~" . a:ClassName ."(void)\<cr>{\<cr>"
:execute "normal! a\<tab>return ;\<cr>"
:execute "normal! a}"
"Comment this line if you dont want to save files straight away.
":execute 'write'
"================== editing header file =================================
execute "vsp %:h/" . a:ClassName . ".class.hpp"
"At this stage the autocomands for this filetype are done.
" example: inserting the header, and the ifndef... Then:
:execute "normal! a" . "class " . a:ClassName ."\<cr>{\<cr>"
:execute "normal! a\<tab>public:\<cr>"
:execute "normal! a\<tab>\<tab>" . a:ClassName . "(void);\<cr>"
:execute "normal! a\<tab>\<tab>~" . a:ClassName . "(void);\<cr>\<cr>"
:execute "normal! a\<tab>protected:\<cr>\<cr>"
:execute "normal! a\<tab>private:\<cr>\<cr>"
:execute "normal! a};"
:execute "normal! ka\<tab>\<tab>"
"Comment out this line if you dont want to start in insert mode
:startinsert!
"Comment this line if you dont want to save files straight away.
:execute 'write'
endfunction
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With