簡體   English   中英

如何添加為 C++ 生成類源文件和頭文件的 vim 命令?

[英]How do I add a vim command generating a class source and header files for C++?

當我從VIM 命令行鍵入:Class Toto ,我想獲取標頭和源文件及其模板,就像我們創建新類時任何編輯器所做的那樣。 因此,如果

輸入: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    

打開vim並輸入:call Class("toto")

你的 vim 將被分成 3 部分:

  1. 你當前的文件
  2. toto.h
  3. 帶有上述模板的 toto.cpp

如果你想將命令:call Class("toto"):Class toto在你的~/.vimrc添加這一行:

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

結果 :

在此處輸入圖片說明

這是賽義夫·法迪(Saif Faidi)作品的細微變化。

它帶來的唯一的東西:

  • 在所需位置以插入模式開始。
  • 不使用行號。 這樣插入可以相對於光標所在的位置。 就我而言,在創建擴展名為 .hpp 或 .cpp 的文件時,會插入一些自動標題和包含保護。 然后這些行生效:
    "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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM