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)

TylerH
  • 20,799
  • 66
  • 75
  • 101
Saif Faidi
  • 509
  • 4
  • 15
  • 1
    https://stackoverflow.com/questions/57074531/vim-command-to-insert-multiline-text-with-argument Basically, you need only to add a couple of Ex commands to create/write files/buffers. – Matt Jul 17 '19 at 17:01
  • 1
    Also you could look at creating and using templates with `vim` https://shapeshed.com/vim-templates/#using-templates-in-vim – Léa Gris Jul 17 '19 at 17:16
  • UltiSnip plugin is the bazooka for templates (here called snippets): https://github.com/vim-scripts/UltiSnips – Tinmarino Jul 17 '19 at 21:55

2 Answers2

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

Saif Faidi
  • 509
  • 4
  • 15
  • 1
    I found this to be a really good approach, so i upvoted it. i also proposed a variation i did as well. – CharMstr Dec 11 '20 at 03:05
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
CharMstr
  • 184
  • 2
  • 8