4

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)

3

2 Answers 2

7

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:

  1. you current file
  2. toto.h
  3. toto.cpp with the templates mentioned above

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 :

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I found this to be a really good approach, so i upvoted it. i also proposed a variation i did as well.
1

This is a slight variation of the work of Saif Faidi.

The only thing it brings:

  • Starting in insert mode in the desired spot.
  • Not using line numbers. So that insertions can be relative to where the cursor is. In my case When creating a file with the extension .hpp or .cpp, some automatic headers and inclusion protections are inserted. Then those lines take effect:
    "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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.